c++ - Lambda 作为默认参数失败

标签 c++ c++17

我在使用最新版本的 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/

相关文章:

c++ - move 具有非默认构造类的保护的构造函数

c++ - void 类型的函数参数

c++ - 为什么 C++17 中的全局内联变量和静态内联成员需要守卫?

c++ - 即使没有定义构造函数,也可以通过附加 () 来初始化结构

c++ - 有没有办法将 std::async 与 std::experimental::future 一起使用?

c++ - 结构化绑定(bind)和强制复制省略

使用 lua 的 C++ 工厂

c++ - 为什么派生类中的虚析构函数为空?

C++ 错误 LNK2001,找不到错误

c++ - 寻找新的内存地址? C++