Search code examples
c++boostinitializationptr-vector

Is there a way to Boost.Assign a ptr_vector?


Usually like this:

#include <boost/assign/std/vector.hpp>
vector<int> v;
v += 1,2,3,4,5;

Except for a:

#include <boost/ptr_container/ptr_vector.hpp>
boost::ptr_vector<int> v;

If you need to know the reason; I'm using ptr_vector instead of vector only so I don't have to delete elements, but I need to initialize it using Boost.Assign as I want the ptr_vector to be const (can't use push_back() or pop_back() anywhere else in code.)

Thanks in advance for you answers, it's possible I'm using the wrong container type?


Solution

  • Use Boost.Assigns ptr_list_of():

    #include <boost/assign/ptr_list_of.hpp>
    
    // ...
    const boost::ptr_vector<int> pv = boost::assign::ptr_list_of<int>(1)(2)(3);