使用boost ::分配:: LIST_OF [英] Using boost::assign::list_of

查看:659
本文介绍了使用boost ::分配:: LIST_OF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这编译:

 的std ::矢量<&INT GT;值=提振::分配:: LIST_OF(1)(2);

但不是这样的:

 构造函数(的std ::矢量<&INT GT;值)
{
}构造函数(升压::分配:: LIST_OF(1)(2));

有没有初始化传递给构造函数的矢量一个班轮解决方案?

更妙的是,如果构造复制到一个类变量,采取了参考,而不是:

 构造函数(的std ::矢量<&INT GT;&安培;价值)
{
    _value =值;
}

更新

如果我尝试以下方法:

 枚举富
{
    FOO_ONE,FOO_TWO
};类的构造函数
{
上市:
    构造函数(常量的std ::矢量<&符GT;&安培;价值){}
};构造函数C(性病::矢量<&符GT;(升压::分配:: LIST_OF(FOO_ONE)));

我得到的编译器错误:

 错误C2440:'<作用式铸造>' :不能从转换'的boost :: assign_detail :: generic_list< T>'为'的std ::矢量<&_Ty GT;'
1>同
1> [
1> T =富
1> ]
1>和
1> [
1> _Ty =富
1> ]
1>没有构造函数可采取源类型,或构造函数重载决议是暧昧


解决方案

这是一个恼人的问题,我们也有一些时间了。我们通过使用 convert_to_container 方法固定它:

 构造函数C(提高::分配:: LIST_OF(1)(2).convert_to_container<的std ::矢量<&INT GT;>());

有更多的问题是的std ::列表使用构造函数了。见<一href=\"http://stackoverflow.com/questions/20135896/pass-stdlist-to-constructor-using-boosts-list-of-doesnt-compile\">Pass的std ::列表中使用提升的LIST_OF不编译以适当的答案构造函数。

This compiles:

std::vector<int> value = boost::assign::list_of(1)(2);

But not this:

Constructor(std::vector<int> value)
{
}

Constructor (boost::assign::list_of(1)(2));

Is there a one-liner solution for initializing the vector passed to the constructor?

Better still, if the constructor copies to a class variable by taking a reference instead:

Constructor(std::vector<int>& value)
{
    _value = value;
}

UPDATE

If I try the following:

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

I get the compiler error:

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

解决方案

This is a annoying problem, we also had some time before. We fixed it by using the convert_to_container method:

Constructor c(boost::assign::list_of(1)(2).convert_to_container<std::vector<int> >() );

There are more issues with std::list using in constructor too. See Pass std::list to constructor using boost's list_of doesn't compile for the appropriate answer.

这篇关于使用boost ::分配:: LIST_OF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆