Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-users] [assign] std::vector initialisation by boost::assign::list_of
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2010-03-17 11:16:12


Actually I tested some of them with gcc and the only solution I came up to
resolve ambiguity is:

#include <boost/assign.hpp>
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>

using namespace std;
using namespace boost;
using namespace boost::assign;

int main(int argc, char* argv[])
{
  typedef vector<int> int_vec;
  int_vec v = list_of(1)(2);
  int_vec v2(2);
  v2=list_of(1)(2).to_container(v2);

  ostream_iterator<int> oiter(cout, ",");
  copy(v2.begin(), v2.end(), oiter);
}

Hope that helps,

Ovanes

On Wed, Mar 17, 2010 at 1:52 PM, Ovanes Markarian <om_boost_at_[hidden]>wrote:

> Sorry, it must be to_container() and not to_adapter().
>
>
> On Wed, Mar 17, 2010 at 1:51 PM, Ovanes Markarian <om_boost_at_[hidden]>wrote:
>
>>
>> On Wed, Mar 17, 2010 at 1:32 PM, Andrey Torba <andreytorba_at_[hidden]>wrote:
>>
>>> The code is compilable on msvc but is not on gcc-4.3:
>>>
>>> But the problem occurs when i try to initialise std::vector in a class
>>> initialization list:
>>> class sss
>>> {
>>> std::vector<int> v;
>>> sss()
>>> : v(boost::assign::list_of(1)(2))
>>> {}
>>> };
>>>
>>> What happens if you call:
>> sss()
>> : v(boost::assign::list_of(1)(2).to_adapter())
>> {}
>>
>> list_of implements 2 members:
>> implicit conversion opertor to the container type and the to_adapter
>> member. You can cast list_of instance to your vector<int> if that with
>> adapter does not work. Other solution can be:
>>
>> If you need efficiency, you still can use the vector ctor which reserves
>> the number of items in a vector and than copies
>> the elements from the list.
>>
>> sss() : v(2)
>> {
>> v = list_of...;
>> }
>>
>> Regards,
>> Ovanes
>>
>>
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net