std::set has no member emplace

John Dibling picture John Dibling · Apr 4, 2013 · Viewed 10.1k times · Source

Does g++ 4.7.2 implement std::set::emplace, as defined by the C++11 Standard and documented here?

I have written the following little test case:

#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"));
}

Under G++ 4.7.2, this fails to compile:

[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’

Also fails to compile under IDEOne, however it does compile under MSVC 2012 Update 1.

Answer

Ali picture Ali · Apr 4, 2013

It is not implemented in gcc 4.7.2.

There is some explanation:

Just to clarify a bit: this wasn't an oversight. We had the nasty problem in the Draft C++0x Standard with std::pair, which essentially made impossible adding the emplace_* members to std::map, std::multimap, etc, without breaking existing user code. Thus we waited on that, until things got clarified in this whole area. Now it's actually possible to work on those facilities.

Your code compiles nicely with gcc 4.8.0, see LWS.