Using boost.assign for the shared_ptr collection

Consider the following snippet:

class Foo {
public:
    Foo( int Value );

    // other stuff
};

std::list< boost::shared_ptr< Foo > > ListOfFoo = list_of( 1 )( 2 )( 3 )( 4 )( 5 );

This does not work. What is the easiest way to make this work, or is there any method for assigning values ListOfFooas simple as that?

+3
source share
4 answers

boost::assign::ptr_list_ofallows you to build a Boost pointer container with a very simple syntax. You can extend it with private inheritance so that it can create containers shared_ptr:

template< class T > 
struct shared_ptr_list : boost::assign_detail::generic_ptr_list<T>
{
    typedef boost::assign_detail::generic_ptr_list<T> Base;

    template< class Seq >
    operator Seq() const 
    {
        Seq result;
        for(typename Base::impl_type::iterator it = Base::values_.begin(), e = Base::values_.end(); it != e; ++it)
            result.push_back(typename Seq::value_type(&*it));
        Base::values_.release().release();
        return result;
    }     

    template< class U >
    shared_ptr_list& operator()( const U& u )
    {
        return (shared_ptr_list&)boost::assign_detail
               ::generic_ptr_list<T>::operator()(u);
    }    
};

template< class T, class U >
shared_ptr_list<T> shared_ptr_list_of( const U& t )
{
    return shared_ptr_list<T>()(t);
}

It looks a little ugly, but then it is really convenient to use:

int main()
{
    using boost::shared_ptr;
    std::deque<shared_ptr<Foo> > deq = shared_ptr_list_of<Foo>(1)(2)(3);
}
+3
source

Another way would be to use std::transformfor an array of arguments:

const unsigned DataSize = 5;
int data[DataSize] = {1, 2, 3, 4, 5};
std::list<boost::shared_ptr<Foo> > ListOfFoo;
std::transform(data, data + DataSize, std::back_inserter(ListOfFoo), &boost::make_shared<Foo, int>);

, , .

+2
std::list<boost::shared_ptr<Foo> > ListOfFoo = boost::assign::list_of(boost::make_shared<Foo>(1))(boost::make_shared<Foo>(2));

There is no implicit conversion from Foo*to shared_ptr<Foo>.

shared_ptr<Foo> ptr = new Foo(1); // you can't do this
shared_ptr<Foo> ptr(new Foo(1)); // this is ok
shared_ptr<Foo> ptr = make_shared<Foo>(1); // this is also ok

What you want is not possible, you need to explicitly create generic pointers and pass them to list_of.

+1
source

Your boost :: list_of requires type objects boost::shared_ptr<Foo>. Therefore, you can do it as follows:

typedef boost::shared_ptr<Foo> FooPtr;

std::list<boost::shared_ptr<Foo> > fooList = list_of
    (FooPtr(new Foo(1))
    (FooPtr(new Foo(2))
    (FooPtr(new Foo(3));
+1
source

Source: https://habr.com/ru/post/1732851/


All Articles