【发布时间】:2019-10-30 07:37:36
【问题描述】:
我想拥有std::function 的std:array,但我想确保数组的所有元素都已初始化。
为此,我构建了一个将std::function 作为构造参数的包装类。
但是,当我直接使用我的函数(应该在 std::function 中的那个)初始化包装类的数组时,它无法编译。
这是问题,提炼:
#include <functional>
#include <array>
static void f() {}
using F = std::function<void(void)>;
enum { Count = 4 };
struct C
{
//To get a compilation error when some
// elements of the array are not initialized.
C() = delete;
C(F) {}
};
//OK
static const C c {f};
//OK
static const std::array<F,Count> direct
{
F{f},
{f},
f,
f
};
static const std::array<C,Count> wrapper
{
F{f}, //OK
C{f}, //OK
{f}, //OK
f //could not convert 'f' from 'void()' to 'C'
};
我尝试将数组更改为 std::vector<C>(尽管它违背了我使用 std:array 开头的全部目的)并且它拒绝编译上述任何初始化。
【问题讨论】:
标签: c++ c++11 initialization std-function stdarray