簡體   English   中英

使用boost :: assign :: list_of構造std :: vector時的歧義

[英]Ambiguity when using boost::assign::list_of to construct a std::vector

這段代碼:

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

給出錯誤:

main.cpp: In member function 'void <unnamed>::RequestHandler::processRequest(Foo&, Bar, unsigned int, unsigned int*, const char*, boost::shared_ptr<Baz::IOutput>&)':
main.cpp:450: error: call of overloaded 'vector(boost::assign_detail::generic_list<int>&)' is ambiguous
/4.4.2/bits/stl_vector.h:241: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
/4.4.2/bits/stl_vector.h:227: note:                 std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]
/4.4.2/bits/stl_vector.h:215: note:                 std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>]

當為gcc 4.4.2編譯時。

我該如何解決這個問題? 最終我正在嘗試編譯以下內容:

using namespace std;
using namespace boost::assign;

typedef boost::variant<vector<bool>, vector<int>, vector<string> > Container;

vector<pair<string, Container > > v =
            list_of(pair<string, Container >("Scotland",Container(vector<int>(list_of<int>(1)(2)(3)))))
            (pair<string, Container >("Sweden",Container()));

出於同樣的原因,這也失敗了。

我的應用程序涉及為單元測試設置數據結構。 我希望初始化清晰,而不是必須做很多push_backs等。

更新:

這是一個修復:

std::vector<std::pair<std::string, Container > > v =
    boost::assign::list_of(std::pair<std::string, Container >("Scotland",Container(boost::assign::list_of(1)(2)(3).convert_to_container<std::vector<int> >())))
    (std::pair<std::string, Container >("Sweden",Container()));

這太丑了。 我該怎么做才能讓這個更清楚。 我的單元測試只在頭文件中編寫,所以我不使用using namespace

這是VS2010中list_of模糊調用 Boost.Assign是針對C ++ 03標准庫編寫的。 C ++ 11和Boost.Assign中發生的變化尚未更新。 錯誤報告在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM