加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用[]和[this]时lambda类型的差异

发布时间:2020-12-16 10:50:55 所属栏目:百科 来源:网络整理
导读:我有一个类db_interface.并定义了一个lambda类型: typedef void (*db_interface_lambda)(); 当我以这样的方式在类中创建lambda:[](){/ *做某事* /}时,它具有良好的类型(db_interface_lambda),但是当我使用[this](){/ *做某事* /}时,编译器开始对我大喊大叫
我有一个类db_interface.并定义了一个lambda类型:

typedef void (*db_interface_lambda)();

当我以这样的方式在类中创建lambda:[](){/ *做某事* /}时,它具有良好的类型(db_interface_lambda),但是当我使用[this](){/ *做某事* /}时,编译器开始对我大喊大叫.

cannot convert ‘db_interface::db_interface(std::ifstream&)::<lambda()>’ to ‘std::map<std::basic_string<char>,void (*)()>::mapped_type {aka void (*)()}’ in assignment

如何解决这个问题?什么是正确的类型?

解决方法

因为lambdas只能隐式转换为函数指针,当且仅当它们不捕获任何东西时.

§5.1.2[expr.prim.lambda] p6

The closure type for a lambda-expression with no lambda-capture ([] is empty) has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator.

顺便说一下,你输入的是什么,有一个函数指针,而不是lambda类型. Lambda表达式具有唯一的,未命名的,不连续的类类型.你不能命名他们.

§5.1.2[expr.prim.lambda] p3

The type of the lambda-expression (which is also the type of the closure object) is a unique,unnamed nonunion class type

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读