【问题标题】:std::map emplace gcc 4.8.2std::map 安放 gcc 4.8.2
【发布时间】:2014-08-14 20:32:59
【问题描述】:

我正在尝试使用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 版本中实现的吗?

【问题讨论】:

  • 你可能需要用-std=c++11之类的编译。
  • 请粘贴完整的编译器输出。

标签: c++11 map emplace


【解决方案1】:

这是一些源代码:

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

【讨论】:

  • 我正在编译 -std=c++11 和 gcc 4.8.2 但它不起作用。
  • 你能告诉我/opt/gcc/4.8.2/bin/g++ -std=c++11 -v foo.cc的完整输出吗
  • scons:构建目标... g++ -o build/parameter_reader.o -c -O0 -std=c++11 -g -Isrc build/parameter_reader.cpp build/parameter_reader.cpp:在成员函数 'bool lapsense::ParameterReader::readParameterLine()': build/parameter_reader.cpp:56:24: error: 'class std::map<:basic_string>, double>' 没有名为 ' emplace' this->parameterList->emplace(splitVec[0], value); ^ 在 build/parameter_reader.cpp:2:0: 中包含的文件中
  • @Thorsten:你可以复制我帖子中的简短示例,并在我的帖子中使用命令行进行编译吗?值得注意的是,它在参数中包含-v。它应该打印出大约 30 行输出。您可以将其添加到您的帖子中,也可以将其上传到 gist.github.com 之类的地方。
  • @Thorsten:为了澄清我的要求,你刚刚给我的与我的要求几乎没有关系。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2015-01-18
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2012-09-28
  • 2013-12-02
相关资源
最近更新 更多