此常见问题解答涉及聚合和POD,并涵盖以下材料:
您好我正在学习C++ 11,我想知道如何制作一个constexpr 0到n数组,例如:
n = 5;
int array[] = {0 ... n};
Run Code Online (Sandbox Code Playgroud)
所以阵列可能是 {0, 1, 2, 3, 4, 5}
以下文章是我为模板参数包找到的第一个提案.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf
在第16页,它讨论了引入两个新的运算符[]和<>来访问参数包元素和参数包类型.
The suggested syntax for such an operator involves two new operators: .[] to access values and .<> to access types. For instance:
template<int N, typename Tuple> struct tuple_element;
template<int N, ... Elements>
struct tuple_element<tuple<Elements...> >
{
typedef Elements.<N> type;
};
template<int N, ... Elements>
Elements.<N>& get(tuple<Elements...>& t)
{ return t.[N]; }
template<int N, ... Elements>
const Elements.<N>& get(const tuple<Elements...>& t)
{ return t.[N]; }
Run Code Online (Sandbox Code Playgroud)
那么这些运营商在哪里?如果没有,他们的替代品是什么?
我开始使用c ++ 11,constexpr和模板元编程似乎是一种在微型微控制器上保存稀缺内存的好方法.
有没有办法编写模板来展平constexpr数组列表,我需要的是一种方法:
constexpr std::array<int, 3> a1 = {1,2,3};
constexpr std::array<int, 2> a2 = {4,5};
constexpr auto a3 = make_flattened_array (a1,a2);
Run Code Online (Sandbox Code Playgroud)
我使用gcc 4.8.4(arm-none-eabi),如果需要,可以使用std = c ++ 11或c ++ 1y选项进行编译.
我正在寻找创建一个坐标查找表,如:
int a[n][2] = {{0,1},{2,3}, ... }
Run Code Online (Sandbox Code Playgroud)
对于给定的n,在编译时创建.我开始研究constexpr,但似乎是一个函数返回a constexpr std::vector<std::array <int, 2> >不是一个选项,因为我得到:
invalid return type 'std::vector<std::array<int, 2ul> >' of constexpr function
Run Code Online (Sandbox Code Playgroud)
如何创建这样的编译时数组?