加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 14 lambda的默认参数类型推导取决于前面的参数

发布时间:2020-12-16 10:13:46 所属栏目:百科 来源:网络整理
导读:这不是C 14的有效期吗? auto f = [](auto x,auto y = std::decay_tdecltype(x){}) { };f(0); 我期待它大致相当于 auto f = [](int x,int y) { };f(0,int{}); GCC 6.3和Clang 4.0都没有接受我的代码. http://ideone.com/b7b4SK海湾合作委员会 http://ideone.
这不是C 14的有效期吗?

auto f = [](auto x,auto y = std::decay_t<decltype(x)>{}) { };
f(0);

我期待它大致相当于

auto f = [](int x,int y) { };
f(0,int{});

GCC 6.3和Clang 4.0都没有接受我的代码.

> http://ideone.com/b7b4SK海湾合作委员会
> http://ideone.com/EyLYaL Clang

是否与我对C模板扣除阶段缺乏了解有关? 1400页长规格实际上对我的问题有明确的答案吗?

更新

总而言之,我的问题实际上可以简化为这段代码(不含lambda,单个参数),并且在C 14下无效(感谢@BaummitAugen和@NirFriedman)

template <typename T>
void f(T x = 0) { }

int main() {
    f();
}

解决方法

编译器拒绝你的代码是正确的,它确实无效C 14.

在标准中(在这里使用N4141)我们有

For a generic lambda,the closure type has a public inline function call
operator member template (14.5.2) whose template-parameter-list consists of one invented type template-
parameter for each occurrence of auto in the lambda’s parameter-declaration-clause,in order of appearance.

(5.1.2 / 4 [expr.prim.lambda]).所以你的电话相当于打电话给某些人

template <class T1,class T2>
auto operator() (T1 x,T2 y = std::decay_t<decltype(x)>{});

现在

If a template parameter is used only in non-deduced
contexts and is not explicitly specified,template argument deduction fails.

(14.8.2 / 4 [temp.deduct.type])和

The non-deduced contexts are:
[…]
– A template parameter used in the parameter type of a function parameter that has a default argument
that is being used in the call for which argument deduction is being done.

(14.8.2 / 5 [temp.deduct.type])使你的电话不正常.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读