std :: map emplace gcc 4.8.2

时间:2014-06-24 14:18:34

标签: c++11 map emplace

我正在尝试使用std :: map的emplace函数,但它似乎没有实现(但我读过它是在4.8中实现的)

以下代码:

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

导致:

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

有人可以澄清执行emplace函数的gcc版本吗?

1 个答案:

答案 0 :(得分:6)

这里有一些源代码:

#include <map>
#include <string>

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

让我们尝试编译它:

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

请注意,当我们使用-std=c++11时,它会起作用。这是因为std::map::emplace()是2011 C ++标准中添加的一项功能。

此外,我可以验证:

  • g ++ 4.7.3不支持std::map::emplace()
  • g ++ 4.8.0支持std::map::emplace()