我在使用最新版本的 clang 和 gcc 时遇到错误:
int main() {
auto lambda = [] (auto = [] {}) {};
lambda();
}
Clang 给出错误:
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();
^
为什么会失败?
最佳答案
auto
的类型推导不考虑默认参数。
关于c++ - Lambda 作为默认参数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30240131/