C++ 构造函数中的lambda实例化
我有这样一个构造器:C++ 构造函数中的lambda实例化,c++,lambda,constructor,C++,Lambda,Constructor,我有这样一个构造器: ConcurrentHashMap(int expected_size, int expected_threads_count, const Hash& hasher = Hash()) { this->my_hash_ = hasher; if (expected_size != kUndefinedSize) table.reserve(expected_size); } 当我为hasher参数传
ConcurrentHashMap(int expected_size, int expected_threads_count, const Hash& hasher = Hash())
{
this->my_hash_ = hasher;
if (expected_size != kUndefinedSize)
table.reserve(expected_size);
}
当我为hasher
参数传递lambda函数时:
auto lambda = [](const std::pair<int, int>& x) {
return pair_hash(x);
};
auto lambda=[](常数std::pair&x){
返回对_散列(x);
};
我得到了错误:
: In instantiation of ‘ConcurrentHashMap<K, V, Hash>::ConcurrentHashMap(int, int, const Hash&) [with K = std::pair<int, int>; V = std::__cxx11::basic_string<char>; Hash = Correctness_Constructors_Test::TestBody()::<lambda(const std::pair<int, int>&)>]’:
required from here
:在“ConcurrentHashMap::ConcurrentHashMap(int,int,const Hash&)”的实例化中[K=std::pair;V=std::\uuuuucx11::basic\u string;Hash=correctibility\u Constructors\u Test::TestBody():]:
从这里开始需要
以及:
错误:使用已删除的函数“correction\u Constructors\u Test::TestBody():()”
如何克服此问题?这里的问题是您在构造函数成员初始化列表中默认构造了
my_hash\uu
(因为您没有提供),然后在构造函数体中为其赋值。由于my_hash\uu
是lambda,因此它不是默认可构造的,因为lambda不是默认可构造的。您需要在成员初始值设定项列表中初始化my\u hash\uu
,如下所示
ConcurrentHashMap(int expected_size, int expected_threads_count,
const Hash& hasher = Hash()) : my_hash_(hasher)
{
//...
}
Hash
是什么样子的?请发布一个。lambda不是默认可构造的,您正在尝试在某个地方(可能在默认参数中)默认构造一个lambda。
ConcurrentHashMap(int expected_size, int expected_threads_count,
const Hash& hasher = Hash()) : my_hash_(hasher)
{
//...
}