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

标签 c++ c++11 std

我有一个函数签名如下:

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/

相关文章:

c++ - 允许复制列表初始化和显式构造函数吗?

c++ - C++中的转发参数

c++ - std::shared_ptr 线程安全

c - 使用 MINGW 强制 C 将 stdin 读取为二进制

c++ - 多个线程执行写入?

c++ - 为什么需要强制转换为 bool 值?

c++ - 在 Windows 上强制刷新 ofstream 文件

c++ - std::max() 和 std::min() 不是 constexpr

c++11 - Apple clang-703.0.29 如何在 C++1x 支持方面映射回 clang 版本?

c++ - 带有 std::vector 和带有缩减的标量变量的 OpenMP for 循环