decltype中的C ++ 11 lambda [英] C++11 lambda in decltype

查看:229
本文介绍了decltype中的C ++ 11 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

auto F(int count) -> decltype([](int m) { return 0; }) 
{                                                               
    return [](int m) { return 0; };                                  
}

g ++ 4.5会显示错误:

g++ 4.5 gives the errors:

test1.cpp:1:32: error: expected primary-expression before 'int'
test1.cpp:1:32: error: expected ')' before 'int'

有什么问题?从函数返回lambda的正确方法是什么?

What is the problem? What is the correct way to return a lambda from a function?

推荐答案

除非实际创建该对象,否则不能使用lambda表达式 - 这使得它不可能传递类型的dedionction像decltype。

You cannot use a lambda expression except by actually creating that object- that makes it impossible to pass to type deduction like decltype.

讽刺的是,lambda返回规则使得你可以从lambdas返回lambdas,因为那里在某些情况下,不必指定返回类型。

Ironically, of course, the lambda return rules make it so that you CAN return lambdas from lambdas, as there are some situations in which the return type doesn't have to be specified.

只有两个选项 - 返回一个多态容器,例如 std :: function ,或者使F本身为实际的lambda。

You only have two choices- return a polymorphic container such as std::function, or make F itself an actual lambda.

auto F = [](int count) { return [](int m) { return 0; }; };

这篇关于decltype中的C ++ 11 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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