gpt4 book ai didi

c++ - std::set 没有成员 emplace

转载 作者:IT老高 更新时间:2023-10-28 13:00:31 24 4
gpt4 key购买 nike

g++ 4.7.2 是否实现std::set::emplace,由C++11 标准定义并记录here ?

我写了以下小测试用例:

#include <set>
#include <string>

struct Foo
{
std::string mBar;
bool operator<(const Foo& rhs) const
{
return mBar < rhs.mBar;
}
Foo(const std::string bar) : mBar(bar) {};
};

typedef std::set<Foo> Foos;

int main()
{
Foos foos;
foos.emplace(std::string("Hello"));
}

在 G++ 4.7.2 下,编译失败:

[john.dibling@somewhere hacks]$ g++ -o main.o -std=c++0x -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:19:10: error: ‘Foos’ has no member named ‘emplace’

IDEOne下也编译失败,但是它确实在 MSVC 2012 Update 1 下编译。

最佳答案

It is not implemented in gcc 4.7.2.

有一些解释:

Just to clarify a bit: this wasn't an oversight. We had the nasty problem in the Draft C++0x Standard with std::pair, which essentially made impossible adding the emplace_* members to std::map, std::multimap, etc, without breaking existing user code. Thus we waited on that, until things got clarified in this whole area. Now it's actually possible to work on those facilities.

您的代码在 gcc 4.8.0 上编译得很好,参见 LWS .

关于c++ - std::set 没有成员 emplace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15812276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com