将std :: array传递给函数:默认值

时间:2014-11-14 12:34:34

标签: c++ c++11 std

我有一个函数签名如下:

void analyze(Image * x, std::array<bool, 4> 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})));

无论我尝试什么,我都无法编译。上面的例子说“错误:预期的表达”。

2 个答案:

答案 0 :(得分:0)

这有效:

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});

使用初始化列表初始化数组。你的代码有额外的括号。

答案 1 :(得分:0)

听起来这是你的编译器问题。 这是来自C ++ 11之前的解决方案:

void analyze(Image* x, std::bitset<4> smooth = std::bitset("1110"));

您还可以使用std::bitset初始化unsigned long longstd::bitset(14ull)

相关问题