【问题标题】:Incorrect copy constructor while passing argument to generic lambda将参数传递给通用 lambda 时复制构造函数不正确
【发布时间】:2018-09-27 08:06:10
【问题描述】:

考虑这段代码:

template < auto What >
constexpr auto Identity = [](auto&&...) { return What; };

struct Ban
{
  Ban() = default;
  Ban(const Ban&  ban) = delete;
  Ban(      Ban&& ban) = delete;
};

int main()
{ 
  Ban ban;
  Identity<false>(10,ban);

  return 0;
}

当它试图复制Identity 的第二个参数时,无法在godbolt.orggcc-7.3 上编译。为什么应该?这是gcc 中的错误吗?

gcc 在第二个参数是临时参数或只有一个参数时不会抱怨。当Identity 的定义是(...) 而不是(auto&amp;&amp;...) 时,它只抱怨一个参数。

【问题讨论】:

  • 可能确实是个bug,gcc-8和clang愉快的编译了这个。
  • 令人惊讶的是,gcc-8 在将 (auto&amp;&amp;...) 更改为 (...) 时失败了!

标签: c++ gcc c++17 auto generic-lambda


【解决方案1】:

前半部分是对旧 GCC 版本中通用 lambdas 的 auto&amp;&amp;... 的错误解析:clang vs gcc - empty generic lambda variadic argument pack; Should non-capturing generic lambdas decay to function pointers?; Should non-capturing generic lambdas decay to function pointers?

预计下半年。传递 C 风格的 ... 可变参数会生成一个副本,然后您删除了复制构造函数。

【讨论】:

  • 作为建议,包括简单的修复:[](auto&amp;&amp;...x) 为未使用的包命名。
猜你喜欢
  • 2012-09-11
  • 2018-08-31
  • 1970-01-01
  • 2011-12-22
  • 2016-07-19
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多