4

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?

Thorsten
  • 333
  • 1
  • 5
  • 20

1 Answers1

7

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().
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • i am compilying with the -std=c++11 and gcc 4.8.2 but it is not working. – Thorsten Jun 24 '14 at 14:39
  • Can you show me the complete output of `/opt/gcc/4.8.2/bin/g++ -std=c++11 -v foo.cc` – Bill Lynch Jun 24 '14 at 14:40
  • scons: Building targets ... g++ -o build/parameter_reader.o -c -O0 -std=c++11 -g -Isrc build/parameter_reader.cpp build/parameter_reader.cpp: In member function 'bool lapsense::ParameterReader::readParameterLine()': build/parameter_reader.cpp:56:24: error: 'class std::map, double>' has no member named 'emplace' this->parameterList->emplace(splitVec[0], value); ^ In file included from build/parameter_reader.cpp:2:0: – Thorsten Jun 24 '14 at 15:05
  • @Thorsten: Can you copy the short example in my post, and compile it with the command line in my post? Notably, it includes `-v` in the arguments. It should print out about 30 lines of output. You can either add it to your post, or upload it to something like [gist.github.com](http://gist.github.com). – Bill Lynch Jun 24 '14 at 15:07
  • @Thorsten: And just to clarify my request, what you just gave me has very little to do with what I asked for. – Bill Lynch Jun 24 '14 at 15:16
  • here it is: https://gist.github.com/anonymous/2aaf73fc01e00fdc1759 this one compiles – Thorsten Jun 24 '14 at 15:27
  • @Thorsten: Then you have the exciting problem of having your reduced test case not match your actual source code. You'll need to figure out why the code you've posted in the question differs from your actual code. – Bill Lynch Jun 24 '14 at 15:31
  • yeah that's what I thought, will have to check the build script. Thanks for the help. – Thorsten Jun 24 '14 at 15:35