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

c – 使用[]和[this]时lambda类型的差异,第1张

概述我有一个类db_interface.并定义了一个lambda类型: typedef void (*db_interface_lambda)(); 当我以这样的方式在类中创建lambda:[](){/ *做某事* /}时,它具有良好的类型(db_interface_lambda),但是当我使用[this](){/ *做某事* /}时,编译器开始对我大喊大叫. cannot convert ‘db_i 我有一个类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

总结

以上是内存溢出为你收集整理的c – 使用[]和[this]时lambda类型的差异全部内容,希望文章能够帮你解决c – 使用[]和[this]时lambda类型的差异所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1223249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存