问题描述
对于 c++20,建议为通用 lambdas 添加以下语法 p0428r2.pdf
For c++20 it is proposed to add the following syntax for generic lambdas p0428r2.pdf
auto f = []<typename T>( T t ) {};
但是当前在 gcc 8 中的实现不接受以下实例化:
But the current implementation in gcc 8 did not accept the following instantiation:
f<std::string>("");
这是 gcc 中的实现错误还是缺少语言功能?我知道我们谈论的是提案而不是批准的规范.
Is that a implementation bug in gcc or a missing language feature? I know we talk about a proposal and not a approved specification.
完整示例(与模板函数语法比较):
Complete example ( with comparison to template function syntax ):
template <typename T> void n( T t ) { std::cout << t << std::endl; }
auto f = []<typename T>( T t ) { std::cout << t << std::endl; };
int main()
{
f<std::string>("Hello"); // error!
n<std::string>("World");
}
抱怨以下错误:
main.cpp:25:22: 错误:'>' 标记前的预期主表达式f("你好");
main.cpp:25:22: error: expected primary-expression before '>' token f("Hello");
推荐答案
lambda 表达式的结果不是函数;它是一个函数对象.也就是说,它是一个具有 operator() 重载的类类型.所以这个:
The result of a lambda expression is not a function; it is a function object. That is, it is a class type that has an operator() overload on it. So this:
auto f = []<typename T>( T t ) {};
相当于:
struct unnamed
{
template<typename T>
void operator()(T t) {}
};
auto f = unnamed{};
如果你想显式地为 lambda 函数提供模板参数,你必须显式调用 operator():f.operator()(parameters);代码>.
If you want to explicitly provide template arguments to a lambda function, you have to call operator() explicitly: f.operator()<template arguments>(parameters);.
这篇关于通用 lambda 的熟悉模板语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)