首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >构造函数中的lambda实例化

构造函数中的lambda实例化
EN

Stack Overflow用户
提问于 2017-02-25 02:40:32
回答 1查看 320关注 0票数 1

我有这样一个构造函数:

代码语言:javascript
复制
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函数时:

代码语言:javascript
复制
auto lambda = [](const std::pair<int, int>& x) {
    return pair_hash(x);
};

我得到了错误:

代码语言:javascript
复制
: 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

和:

代码语言:javascript
复制
error: use of deleted function ‘Correctness_Constructors_Test::TestBody()::<lambda(const std::pair<int, int>&)>::<lambda>()’

我该如何克服这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-25 02:52:58

这里的问题是,您在构造函数成员初始化列表中默认构造my_hash_ (因为您没有提供),然后在构造函数主体中为其赋值。因为my_hash_是一个lambda,所以它不是默认可构造的,因为lambda不是默认的可构造的。您需要在成员初始化器列表中初始化my_hash_,如下所示

代码语言:javascript
复制
ConcurrentHashMap(int expected_size, int expected_threads_count, 
                  const Hash& hasher = Hash()) : my_hash_(hasher)
{
    //...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42446036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档