相关疑难解决方法(0)

无法从lambda函数中推导出模板参数std :: function

在使用C++浏览模板时,我偶然发现了以下代码中的示例:

#include <iostream>
#include <functional>

template <typename T>
void call(std::function<void(T)> f, T v)
{
    f(v);
}

int main(int argc, char const *argv[])
{
    auto foo = [](int i) {
        std::cout << i << std::endl;
    };
    call(foo, 1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

要编译这个程序,我使用的是GNU C++编译器 g ++:

$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026
Run Code Online (Sandbox Code Playgroud)

编译C++ 11后,我收到以下错误:

$ g++ -std=c++11 template_example_1.cpp -Wall

template_example_1.cpp: In function ‘int main(int, const char**)’:
template_example_1.cpp:15:16: error: no matching function for call to ‘call(main(int, const …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates c++11 c++17

2
推荐指数
1
解决办法
236
查看次数

标签 统计

c++ ×1

c++11 ×1

c++17 ×1

lambda ×1

templates ×1