6

これが機能しない理由がわかりません (Visual C++ 2012):

#include <string>
#include <utility>
#include <vector>
#include <boost/assign/list_of.hpp>

using namespace std;

int main()
{
    pair<string, vector<string> >("^", boost::assign::list_of<string>("rules"));
}

エラーは次のとおりです。

include\utility(138) : error C2668: 'std::vector<_Ty>::vector' : ambiguous call to overloaded function with [ _Ty=std::string ]
include\vector(786): could be 'std::vector<_Ty>::vector(std::vector<_Ty> &&)' with [ _Ty=std::string ]
include\vector(693): or       'std::vector<_Ty>::vector(unsigned int)' with [ _Ty=std::string ]
while trying to match the argument list '(boost::assign_detail::generic_list<T>)' with [ T=std::string ]
test.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const char(&)[2],boost::assign_detail::generic_list<T>>(_Other1,_Other2 &&,void **)' being compiled
with
[
    _Ty1=std::string,
    _Ty2=std::vector<std::string>,
    T=std::string,
    _Other1=const char (&)[2],
    _Other2=boost::assign_detail::generic_list<std::string>
]
test.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const char(&)[2],boost::assign_detail::generic_list<T>>(_Other1,_Other2 &&,void **)' being compiled
with
[
    _Ty1=std::string,
    _Ty2=std::vector<std::string>,
    T=std::string,
    _Other1=const char (&)[2],
    _Other2=boost::assign_detail::generic_list<std::string>
]

オーバーロードにアクセスしようとしている理由を解読できませんunsigned int...何かアイデアはありますか?

4

2 に答える 2

6

これは、pairユニバーサル参照を受け入れるために、C++11 に新しいコンストラクターが追加されたためです。その結果、このコードは VS2012 (このコンストラクターを追加した) および C++11 モードの GCC で失敗します。

C++03 の場合:

pair<T1,T2>コンストラクターは次のとおりです。

pair( const T1& x, const T2& y ) : first(x), second(y) {}

この場合、T2 == vector<string>.

generic_listオブジェクト ( によって返されるオブジェクト)list_ofには、テンプレート変換演算子があります。

template <class Container>
operator Container() const;

パラメーターとして渡すと、オブジェクトをgeneric_listに変換しようとします。これは、コンストラクターが期待するものであり、これが成功するためです。generic_listvector<string>

C++11 の場合:

このpair<T1,T2>コンストラクタが追加されました:

template< class U1, class U2 >
pair( U1&& x, U2&& y ) : first(std::forward<U1>(x)), second(std::forward<U2>(y))

オブジェクトを渡すとgeneric_list、 として渡されgeneric_list&&ます。secondこのオブジェクトで(タイプの) コンストラクターを呼び出そうとするとvector<string>、これらのコンストラクターのどれを呼び出すべきかわかりません。

explicit vector(size_type count, [more params with default values])
vector(const vector& other);

と の両方にgeneric_list変換できるため。これにより、コンパイル エラーが発生します。size_typevector<string>

修正/回避策:

考えられる修正は、convert_to_containerメソッドを使用してターゲット タイプを指定することです。

pair<string, vector<string> >("^", boost::assign::list_of<string>("rules").convert_to_container<vector<string> >());

もう 1 つのオプションは、make_pairそのテンプレート パラメーターを使用して明示的に指定することです。

于 2012-12-18T10:07:28.997 に答える
1

したがって、これの代わりに:

("^", boost::assign::list_of<string>("rules"))

私は書く必要があります:

("^", boost::assign::list_of<string>("rules").convert_to_container<vector<string> >());

ちょっと判読不能にします...さらに別のテンプレートを追加しました:

template <typename T>
std::vector<T> vect(const boost::assign_detail::generic_list<T>& gen_list)
{ return gen_list.convert_to_container<std::vector<T> >(); }

次のように記述できます。

("^", vect(boost::assign::list_of<string>("rules")))

これはまだ良くありませんが、あなたが始めたものに近づいています。

于 2013-06-06T09:58:47.440 に答える