fsc*_*enm 10 c++ lambda map c++11 c++14
我有以下简单示例,其中我想调用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 */ });
}
Run Code Online (Sandbox Code Playgroud)
如果我把所有东西放进去std::vector,它就像我预期的那样工作,但是当使用a时std::map,突然std::for_each想要调用(删除的)复制构造函数.为什么?我原以为我只是获得了对地图中保存的对的引用,没有任何必要的副本.
Tem*_*Rex 15
问题是它std::map有一个std::pair<const Key, Value>内部值类型.标准库容器允许您从容器类型中提取它,而不是显式指定它:
在C++ 11中(与C++ 98相同,但你必须使用函数对象而不是lambda里面for_each,而且还要使用typedef而不是using =):
using value_type = std::map<int, A>::value_type;
std::for_each(begin(m), end(m), [](value_type const& a) { /* do nothing */ });
Run Code Online (Sandbox Code Playgroud)
在C++ 14中:
std::for_each(begin(m), end(m), [](auto const& a) { /* do nothing */ });
Run Code Online (Sandbox Code Playgroud)
autoclang 3.4,Visual Studio 2013 November CTP和GCC 4.9支持lambda内部的使用.