Using boost::assign::list_of

后端 未结 2 1685
野的像风
野的像风 2020-12-18 23:39

This compiles:

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

But not this:

Constructor(std::vector

        
相关标签:
2条回答
  • 2020-12-18 23:55

    I'm using this template to make temporary instance of std::vector in-place:

    #include <vector>
    namespace Util {
    //init vector
    template <typename ELEMENT_TYPE > struct vector_of
        : public std::vector<ELEMENT_TYPE>
    {
        vector_of(const ELEMENT_TYPE& t)
        {
            (*this)(t);
        }
        vector_of& operator()(const ELEMENT_TYPE& t)
        {
            this->push_back(t);
            return *this;
        }
    };
    }//namespace Util
    

    Usage would look like this:

    Constructor (Util::vector_of<int>(1)(2));
    

    Constructor signature would look like this:

    Constructor(const std::vector<int>& value)
    {
        _value = value;
    }
    
    0 讨论(0)
  • 2020-12-18 23:58

    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.

    0 讨论(0)
提交回复
热议问题