Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c++ - Using boost::assign::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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...