Lambda作为默认参数失败 [英] Lambda as default argument fails

查看:136
本文介绍了Lambda作为默认参数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码得到clang和gcc的最新版本的错误:

I get an error with the latest versions of clang and gcc with this code:

int main() {
    auto lambda = [] (auto = [] {}) {};
    lambda();
}

Clang出现错误:

Clang gives the error:

prog.cc: In function 'int main()':
prog.cc:3:12: error: no match for call to '(main()::<lambda(auto:1)>) ()'
     lambda();
            ^
prog.cc:2:35: note: candidate: template<class auto:1> main()::<lambda(auto:1)>
     auto lambda = [] (auto = [] {}) {};
                                   ^
prog.cc:2:35: note:   template argument deduction/substitution failed:
prog.cc:3:12: note:   couldn't deduce template parameter 'auto:1'
     lambda();
            ^

为什么会失败?

推荐答案

自动的类型扣除不考虑默认参数。

Type deduction for auto does not consider default arguments.

这篇关于Lambda作为默认参数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆