我正在尝试使用std::make_unique
来实例化一个需要接收std::initializer_list
的类的构造函数。这里是一个最小化的示例:
#include <string>
#include <vector>
#include <initializer_list>
#include <memory>
struct Foo {
Foo(std::initializer_list<std::string> strings) : strings(strings) {}
std::vector<std::string> strings;
};
int main(int, char**) {
auto ptr = std::make_unique<Foo>({"Hello", "World"});
return 0;
}
您可以在Coliru上看到,它无法构建:
main.cpp:14:56: error: no matching function for call to 'make_unique(<brace-enclosed initializer list>)'
auto ptr = std::make_unique<Foo>({"Hello", "World"});
那么,据报道,make_unique
无法使用initializer_list
吗?GCC 4.9.1中是否存在错误?还是我漏看了什么?
make_unique<Foo>(std::initializer_list<std::string>({"Hello", "World"}))
。 - Kerrek SBinitializer_list
绝对是奇怪的小玩意儿。在这种特定情况下,我认为我只需从一个new
调用中构造unique_ptr
。 - Quentinstd::initializer_list
是一个非常糟糕的设计。对此感到抱歉。 - Kerrek SBstd::make_unique<Foo, std::initializer_list<std::string>>({"Hello", "World"})
- Kerrek SB