【问题标题】:Pass std::list to constructor using boost's list_of doesn't compile使用 boost 的 list_of 将 std::list 传递给构造函数不会编译
【发布时间】:2013-11-22 02:29:48
【问题描述】:

我正在尝试这样做:

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++ 得到以下错误:

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编译。

谢谢...

【问题讨论】:

    标签: c++ boost stl constructor


    【解决方案1】:

    代码由 VC++ 2010 编译。我找到了 GCC 的 2 个解决方法(参见下面的代码)。我认为代码没有任何区别,它将由优化的编译器生成,但我更喜欢第一个变体。我不知道转换运算符不起作用的原因。尝试在 boost 论坛上回答。

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

    【讨论】:

    • 有趣的是,b4 不能用 g++ 编译,但 b3 可以。
    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多