我有一个函数签名如下:
void analyze(Image * x, std::array<bool, 4> smooth);
如果用户没有在调用中明确设置它,我想给这个数组 smooth 一个默认值。所以我尝试了类似的方法:
std::array<bool, 4> smooth = std::array<bool, 4>
({true, true, true, false}));
void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>(({true, true, true, false})));
无论我尝试什么,我都无法编译它。上面的例子说“错误:预期的表达”。
最佳答案
这个有效:
void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>({true, true, true, false}));
这有效:
void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>{true, true, true, false});
用初始化列表初始化数组。您的代码有额外的括号。
关于c++ - 将 std::array 传递给函数:默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930158/