Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用visualc+为std::数组指定默认值+;2012?_C++_Visual Studio_Visual Studio 2012_Stdarray - Fatal编程技术网

C++ 如何使用visualc+为std::数组指定默认值+;2012?

C++ 如何使用visualc+为std::数组指定默认值+;2012?,c++,visual-studio,visual-studio-2012,stdarray,C++,Visual Studio,Visual Studio 2012,Stdarray,如何为std::数组指定默认值?比如说 void f(std::array<int, 3> pt = std::array<int, 3>{0, 1, 2}); void f(std::array pt=std::array{0,1,2}); 在查阅了有用的评论之后。我认为这是由于编译器。如何使用VS 2012解决此问题,而不创建类似于std::array MakeArray(…)?的函数,请尝试以下操作 void f(std::array<int, 3>

如何为std::数组指定默认值?比如说

void f(std::array<int, 3> pt = std::array<int, 3>{0, 1, 2});
void f(std::array pt=std::array{0,1,2});
在查阅了有用的评论之后。我认为这是由于编译器。如何使用VS 2012解决此问题,而不创建类似于
std::array MakeArray(…)

的函数,请尝试以下操作

void f(std::array<int, 3> pt = {0, 1, 2});
void f(std::array pt={0,1,2});
或者我会简单地写

void f(std::array<int, 3> = {0, 1, 2});
void f(std::array={0,1,2});
在GCC中,不编译此代码。这似乎是编译器的一个bug。但是,您可以为GCC编写

void f( std::array<int, 3> = std::array<int, 3>( { 1, 2, 3 } ) );
void f(std::array=std::array({1,2,3}));

void f(std::array={{{1,2,3});

您试过了吗?提示:很有效,就像那样。它对你有用吗?你问过你的编译器吗?因为我问了我的问题,他很满意。我用的是vs2012。它给出了编译错误。这些错误是?这在GCC4.8中不适用(调用
f()
会给出一个错误:
数组必须用括号内的初始值设定项初始化)。OP的版本确实有效。@interjay,它在Clang上的工作方式是一样的,但GCC需要双大括号。
void f( std::array<int, 3> = { { 1, 2, 3 } } );