c++ - 在 VS2010 中使用 list_of 调用不明确

标签 c++ visual-c++ boost compiler-errors

我正在尝试将一个项目从 VS2008 转换为 2010,但由于添加了移动构造函数而遇到了问题(并且可能是因为这里有嵌套的 list_of)。以下代码片段显示了错误(在实际代码中,这些构造用于初始化一些静态信息):

enum some_enum {R, G, B};

typedef std::pair<some_enum, some_enum> Enum_Pair;
typedef std::vector<some_enum> Enum_list;
typedef std::pair<Enum_Pair, Enum_list> Some_Struct;
typedef std::list<Some_Struct> Full_Struct;

#define MAKEFULLSTRUCT(First_, Second_, some_enums)\
    (Some_Struct(Enum_Pair(First_, Second_), list_of (some_enums) ))

int main()
{
    int i = G;
    Full_Struct test_struct = list_of
        MAKEFULLSTRUCT(R, R, R).to_container(test_struct);
}

导致

error C2668: 'std::vector<_Ty>::vector' : ambiguous call to overloaded function
with  [_Ty=some_enum]
vector(593): could be 'std::vector<_Ty>::vector(std::vector<_Ty> &&)'
with  [ _Ty=some_enum]
vector(515): or       'std::vector<_Ty>::vector(unsigned int)'
with  [ _Ty=some_enum]
while trying to match the argument list '(boost::assign_detail::generic_list<T>)'
with  [ T=some_enum ]

有什么方法可以在仍然使用 boost::list_of 的情况下解决这个问题吗?还是我必须切换到另一种初始化机制?

最佳答案

这看起来像是 Boost.Assign 中的错误。 list_of 的返回类型具有类型 T 的通用转换,而根本没有尝试约束 T。因此,std::vector 构造函数——采用 std::vector && 的构造函数和采用 unsigned int 的构造函数 - - 同样好,导致歧义。

解决方法是使用 convert_to_container,如下所示:

#define MAKEFULLSTRUCT(First_, Second_, some_enums)\
    (Some_Struct(Enum_Pair(First_, Second_), \
        list_of(some_enums).convert_to_container<Enum_list>()))

关于c++ - 在 VS2010 中使用 list_of 调用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12352692/

相关文章:

c++ - 什么时候调用 CMFCListCtrl::OnGetCellBkColor 函数?

c++ - 阻塞 TCP 套接字 : “send()” return and error handling

c++ - 基于 MSVC 10 范围的 for 循环

c++ - dijkstra 的算法不计算我的 boost 图中顶点的前辈

c++ - 将 boost::log 用于具有额外 'channel' 和 'id' 属性的多线程应用程序的最佳方法是什么

c++ - 如何处理不断发展的 C++ std::namespace?例如:std::tr1::shared_ptr 对比 std::shared_ptr 对比 boost::shared_ptr 对比 boost::tr1::shared_ptr

c++ - 基对象作为接受派生对象的函数的参数

c++ - 什么是匿名对象?

c++ - 重新加载内联 ostringstream 宏

c++ - 编译 boost::math 的性能测试应用程序