Pass std::list to constructor using boost's list_of doesn't compile

梦想的初衷 提交于 2020-01-17 07:36:07

问题


I am trying to do this:

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

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

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&)

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.

Thanks...


回答1:


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;
}


来源:https://stackoverflow.com/questions/20135896/pass-stdlist-to-constructor-using-boosts-list-of-doesnt-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!