std :: set没有成员安置

Joh*_*ing 24 c++ g++ libstdc++ c++11 g++-4.7

g ++ 4.7.2是否实现std::set::emplace,如C++ 11标准所定义并在此处记录

我写了以下小测试用例:

#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"));
}
Run Code Online (Sandbox Code Playgroud)

在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’
Run Code Online (Sandbox Code Playgroud)

也无法在IDEOne下编译,但它在MSVC 2012 Update 1下编译.

Ali*_*Ali 26

它没有在gcc 4.7.2中实现.

有一些解释:

只是澄清一点:这不是疏忽.我们 在草案C++ 0x标准中使用std :: pair时遇到了令人讨厌的问题,这实际上使得不可能将emplace_*成员添加到std :: map,std :: multimap等,而不会破坏现有的用户代码.因此,我们等待,直到整个领域的事情得到澄清.现在它实际上可以在这些设施上工作.

您的代码与gcc 4.8.0很好地编译,请参阅LWS.


Joh*_*nic 5

emplace()libstdc++gcc 4.8.0 添加了关联容器,在gcc 4.7.2下它不起作用.