【问题标题】:std::set has no member emplacestd::set 没有成员 emplace
【发布时间】:2013-03-26 14:10:15
【问题描述】:

g++ 4.7.2 是否实现 std::set::emplace,由 C++11 标准定义并记录在 here

我写了以下小测试用例:

#include <set>
#include <string>

struct Foo
{
    std::string mBar;
    bool operator<(const Foo& rhs) const
    {
        return mBar < rhs.mBar;
    }
    Foo(const std::string bar) : mBar(bar) {};
};

typedef std::set<Foo> Foos;

int main()
{
    Foos foos;
    foos.emplace(std::string("Hello"));
}

在 G++ 4.7.2 下,编译失败:

[john.dibling@somewhere hacks]$ g++ -o main.o -std=c++0x -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:19:10: error: ‘Foos’ has no member named ‘emplace’

IDEOne 下也无法编译,但在 MSVC 2012 Update 1 下可以编译。

【问题讨论】:

    标签: c++ c++11 g++ libstdc++ g++-4.7


    【解决方案1】:

    It is not implemented in gcc 4.7.2.

    有一些解释:

    澄清一下:这不是疏忽。我们有 讨厌的 C++0x 标准草案中带有 std::pair 的问题,本质上是 不可能将 emplace_* 成员添加到 std::map, std::multimap 等,而不会破坏现有的用户代码。因此我们 等待,直到整个地区的事情都得到澄清。现在 实际上可以在这些设施上工作。

    您的代码可以使用 gcc 4.8.0 很好地编译,请参阅 LWS

    【讨论】:

      【解决方案2】:

      emplace() 关联容器已添加到 gcc 4.8.0 的 libstdc++ 中,在 gcc 4.7.2 下它将不起作用。

      【讨论】:

        猜你喜欢
        • 2015-11-14
        • 1970-01-01
        • 2019-09-23
        • 2013-05-15
        • 1970-01-01
        • 2018-02-10
        • 2020-06-17
        • 2012-01-19
        • 1970-01-01
        相关资源
        最近更新 更多