std::map emplace gcc 4.8.2

Thorsten picture Thorsten · Jun 24, 2014 · Viewed 7.9k times · Source

I am trying to use the emplace function of std::map, but it seems it is not implemented (but I read it was implemented in 4.8)

The following code:

std::map<std::string, double> maps;
maps.emplace("Test", 1.0);

leads to:

class std::map<std::basic_string<char>, double>' has no member named 'emplace'

Can someone clarify in which gcc version the emplace functions are implemented?

Answer

Bill Lynch picture Bill Lynch · Jun 24, 2014

Here's some source code:

#include <map>
#include <string>

int main()
{
    std::map<std::string, double> maps;
    maps.emplace("Test", 1.0);
}

Let's try to compile it:

[9:34am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/g++ -std=c++98 foo.cc
foo.cc: In function ‘int main()’:
foo.cc:7:10: error: ‘class std::map<std::basic_string<char>, double>’ has no member named ‘emplace’
     maps.emplace("Test", 1.0);
          ^
[9:34am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/g++ -std=c++11 foo.cc
[9:34am][wlynch@apple /tmp]

Note that when we use -std=c++11 it works. This is because std::map::emplace() is a feature that was added in the 2011 C++ Standard.

Additionally, I can verify that:

  • g++ 4.7.3 does not support std::map::emplace().
  • g++ 4.8.0 does support std::map::emplace().