std::map emplace gcc 4.8.2

大兔子大兔子 提交于 2019-12-10 15:46:08

问题


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?


回答1:


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().


来源:https://stackoverflow.com/questions/24389014/stdmap-emplace-gcc-4-8-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!