通过的std ::列表中使用提升的LIST_OF不编译构造器 [英] Pass std::list to constructor using boost's list_of doesn't compile

查看:125
本文介绍了通过的std ::列表中使用提升的LIST_OF不编译构造器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做到这一点:

class bbb
{
public:
    bbb(std::list<int> lst) { }
};

int main()
{
    bbb b((std::list<int>)boost::assign::list_of<int>(10)(10));
    return 0;
}

和获得以下从G ++错误:

and get following error from g++:

some.cc:35: error: call of overloaded 'bbb(boost::assign_detail::generic_list<int>&)' is ambiguous
some.cc:15: note: candidates are: bbb::bbb(std::list<int, std::allocator<int> >)
some.cc:13: note:                 bbb::bbb(const bbb&)

有没有什么办法来解决这个问题呢?
请注意,我用gcc 4.4.6。它不支持整个C ++ 11,所以我对编译C ++ 03。

Is there any way to work around this problem? Note that I am using gcc 4.4.6. It doesn't support entire c++11, so I am compiling for c++03.

谢谢...

推荐答案

在code是由VC ++ 2010年(见下文code)编译的,我发现2解决方法GCC。我觉得有没有code,这将是优化的编译器生成的任何区别,但我preFER第一个变种。我不知道为什么转换运算符不工作的原因。试着回答提振论坛。

The code was compiled by VC++ 2010. I found 2 workarounds for GCC (See code below). I think there are not any difference in code, which will generated by optimised compiler, but I prefer first variant. I do not know the reason why conversion operator does not work. Try to answer on boost forum.

class bbb
{
 public:
   bbb(std::list<int> lst) { }
};

int main()
{
//simplest decision
   std::list<int> tmp = boost::assign::list_of<int>(10)(10);
   bbb b(tmp);
// one line decision
   bbb b3((boost::assign::list_of<int>(10)(10)).operator std::list<int> () );
//compiled by VC++ 2010 !!!
   bbb b4((std::list<int>)(boost::assign::list_of<int>(10)(10)) );

   return 0;
}

这篇关于通过的std ::列表中使用提升的LIST_OF不编译构造器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆