我有这样一个构造函数:
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);
};我得到了错误:
: 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和:
error: use of deleted function ‘Correctness_Constructors_Test::TestBody()::<lambda(const std::pair<int, int>&)>::<lambda>()’我该如何克服这个问题呢?
发布于 2017-02-25 02:52:58
这里的问题是,您在构造函数成员初始化列表中默认构造my_hash_ (因为您没有提供),然后在构造函数主体中为其赋值。因为my_hash_是一个lambda,所以它不是默认可构造的,因为lambda不是默认的可构造的。您需要在成员初始化器列表中初始化my_hash_,如下所示
ConcurrentHashMap(int expected_size, int expected_threads_count,
const Hash& hasher = Hash()) : my_hash_(hasher)
{
//...
}https://stackoverflow.com/questions/42446036
复制相似问题