2012-11-08 8 views
10

LIST_OFこれは、コンパイルされます。ブーストを使用する::割り当てる::

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

ではなく、これを:

Constructor(std::vector<int> value) 
{ 
} 

Constructor (boost::assign::list_of(1)(2)); 

は、コンストラクタに渡されたベクトルを初期化するためのワンライナーの解決策はありますか?

まだ良い、代わりに参照を取ることによって、クラス変数へのコンストラクタコピーの場合:私は次のことをしようとした場合

Constructor(std::vector<int>& value) 
{ 
    _value = value; 
} 

UPDATE

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))); 

私が手コンパイラエラー:

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 
+0

コンパイラのエラーメッセージを表示できますか? –

+0

@Kevin MOLCARDコンパイラエラー – Baz

+2

を追加しました[これはバグです](https://svn.boost.org/trac/boost/ticket/7364)。 –

答えて

19

これは厄介な問題です。私たちはまたしばらくしていました。私たちは、convert_to_containerメソッドを使用して、それを修正:あまりにもコンストラクタで使用してのstd ::リストとのより多くの問題があります

Constructor c(boost::assign::list_of(1)(2).convert_to_container<std::vector<int> >()); 

。適切な回答については、Pass std::list to constructor using boost's list_of doesn't compileを参照してください。

+0

' conver_to_container'関数を呼び出す必要があるのはなぜですか? –

0

私はSTDの一時的なインスタンスを作るために、このテンプレートを使用しています::インプレースベクトル:

#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 

使い方は次のようになります。

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

コンストラクタのシグネチャは次のようになります。

Constructor(const std::vector<int>& value) 
{ 
    _value = value; 
} 
関連する問題