我正在快速使用C++ 0x,并使用g ++ 4.6进行测试
我只是尝试了下面的代码,认为它会工作,但它不会编译.我收到错误:
incompatible types in assignment of ‘std::initializer_list<const int>’ to ‘const int [2]’
struct Foo
{
int const data[2];
Foo(std::initializer_list<int const>& ini)
: data(ini)
{}
};
Foo f = {1,3};
Run Code Online (Sandbox Code Playgroud) 下面的代码无法编译:
template<typename... Ts>
void CreateArr(const Ts&... args)
{
auto arr[sizeof...(args) + 1]{ args... };
}
int main()
{
CreateArr(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
由于以下错误:
'arr':在直接列表初始化上下文中, for 的类型'auto [6]'只能从单个初始化表达式推导auto [6]':数组的元素类型不能包含'auto''const int'为'std::initializer_list<int>'我的问题是:
为什么我不能使用auto来定义数组的类型?
如何正确定义它以与模板一起使用?