lambda 到函数指针转换的稳定性和唯一性
无捕获 lambda 可以转换为具有与 lambda 表达式相同的参数列表的函数指针。
我想知道这种转换是否保证稳定,即给定一个无捕获的 lambda 表达式,标准是否保证其类型的任何对象的函数指针转换将始终产生相同的指针值?
此外,能否保证该指针值在 lambda 表达式和其他函数中是唯一的?
auto x = []{};
auto x2 = x;
auto y = []{};
assert(+x == +x2); // ?
assert(+x != +y); // ?
A capture-less lambda can be converted to a function pointer with the same parameter list as the lambda expression.
I am wondering whether this conversion is guaranteed to be stable, i.e. given a capture-less lambda expression, is it guaranteed by the standard that the function pointer conversion of any object of its type will always yield the same pointer value?
Furthermore, is it guaranteed that this pointer value is unique among lambda expressions and other functions?
auto x = []{};
auto x2 = x;
auto y = []{};
assert(+x == +x2); // ?
assert(+x != +y); // ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该措辞似乎表明每种类型都有一个函数,因此
assert(+x == +x2);
至少应该成立:这似乎表明有一个函数
F
,并且闭包类型的所有实例都应该转换为该函数。该标准似乎并不要求或禁止不同的闭包类型转换为不同的函数。从表面上看,
+x != +y
似乎可以采用任何一种方式。The wording would seem to suggest that there's one function per type, so
assert(+x == +x2);
at least should hold:This appears to say that there's a single function
F
, and that all instances of the closure type should convert to that function.The standard doesn't seem to require or prohibit that distinct closure types convert to distinct functions. On the surface, it appears that
+x != +y
could go either way.