为什么映射上的std::for_each调用复制构造函数

Why does std::for_each on a map call the copy constructor?

本文关键字:each 调用 复制 构造函数 for std 为什么 映射      更新时间:2023-10-16

我有以下简单的例子,其中我想在不可复制的对象集合上调用std::for_each

class A {
public:
A() : x(0) {}
A(const A&) = delete;
private:
int x;
};
void func() {
std::vector<A> v(10);
std::map<int, A> m;
// works as expected
std::for_each(begin(v), end(v), [](const A& a) { /* do nothing */ });
// error calling copy constructor
std::for_each(begin(m), end(m), [](const std::pair<int, A>& a) { /* do nothing */ });
}

如果我把所有东西都放在std::vector中,它会像我预期的那样工作,但当使用std::map时,std::for_each突然想调用(已删除的)复制构造函数。为什么?我会假设我只是得到了一个保存在地图中的对的引用,而没有任何必要的副本。

问题是std::map的内部值类型为std::pair<const Key, Value>。标准库容器不需要明确指定,而是允许您从容器类型中提取:

在C++11中执行(与C++98中相同,但必须在for_each中使用函数对象而不是lambda,还必须使用typedef而不是using =):

using value_type = std::map<int, A>::value_type;
std::for_each(begin(m), end(m), [](value_type const& a) { /* do nothing */ });

在C++14中做:

std::for_each(begin(m), end(m), [](auto const& a) { /* do nothing */ });

Clang 3.4、Visual Studio 2013 November CTP和GCC 4.9支持在lambda中使用auto