【问题标题】:Ambiguity when using boost::assign::list_of to construct a std::vector使用 boost::assign::list_of 构造 std::vector 时的歧义
【发布时间】:2013-04-25 09:41:55
【问题描述】:

这段代码:

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

【问题讨论】:

  • typedefs(或 C++11 using)为可读性创造了奇迹。
  • @rubenvb 希望它现在更容易阅读......
  • 只是一个旁注,不要使用向量。它专门用于像 bitset 一样,但我相信它有很多问题。 “有效的 STL”,第 18 条,Scott Meyers 不鼓励使用它。
  • @Muckle_ewe 感谢您的提示!

标签: c++ boost c++98


【解决方案1】:

这是Ambiguous call with list_of in VS2010 的骗子。 Boost.Assign 是针对 C++03 标准库编写的。 C++11 中的事情发生了变化,而 Boost.Assign 尚未更新。错误报告是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多