这编译:
std::vector<int> value = boost::assign::list_of(1)(2);
但不是这个:
Constructor(std::vector<int> value)
{
}
Constructor (boost::assign::list_of(1)(2));
是否有一种用于初始化传递给构造函数的向量的单线解决方案?
更好的是,如果构造函数通过引用来复制到类变量:
Constructor(std::vector<int>& value)
{
_value = value;
}
更新
如果我尝试以下操作:
enum Foo
{
FOO_ONE, FOO_TWO
};
class Constructor
{
public:
Constructor(const std::vector<Foo>& value){}
};
Constructor c(std::vector<Foo>(boost::assign::list_of(FOO_ONE)));
我得到编译器错误:
error C2440: '<function-style-cast>' : cannot convert from 'boost::assign_detail::generic_list<T>' to 'std::vector<_Ty>'
1> with
1> [
1> T=Foo
1> ]
1> and
1> [
1> _Ty=Foo
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous