C ++的最烦躁的解析 [英] C++'s most vexing parse again

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

问题描述

直接来自 http://herbsutter.com/2013/05/09/gotw-1 -olution /

虽然 widget w(); 对我来说很清楚,

While widget w(); is clear for me, I have no idea how can the below code be a function declaration?

// same problem (gadget and doodad are types)
//
widget w( gadget(), doodad() );  // pitfall: not a variable declaration

如何实现?

推荐答案

在函数声明中,类型数组的参数衰减为指向第一个元素的指针,类型为function的参数衰减为函数指针,因此签名为:

In a function declaration, arguments of type array decay into pointers to the first element, arguments of type function decay into a function pointer, so the signature would be:

widget w( gadget(*)(), doodad(*)() );

也就是说,一个函数将第一个参数作为指向不带参数的函数的指针, gadget ,它接受第二个参数指向一个没有参数并返回一个 doodad 的函数的指针,函数本身返回小部件

That is, a function that takes as the first argument a pointer to a function taking no arguments and returning gadget, that takes as second argument a pointer to a function taking no arguments and returning a doodad and that the function itself returns a widget

还有更多有趣或混乱的情况,例如:

There are even more interesting or confusing cases, like:

// assume 'x' is a variable defined somewhere:
widget w(gadget(x));

如何将 解释为函数声明?我的意思是, x 是一个变量,对不对?当声明一个变量时,你可以添加额外的括号,所以 gadget x; gadget(x); 相同的变量 x 。这同样适用于函数参数,所以上面的代码看起来像一个函数的声明,它接受 x 的第一个参数 gadget 并返回小部件 ...

How could that be interpreted as a function declaration? I mean, x is a variable, right? Well, when declaring a variable you can add extra parenthesis, so gadget x; and gadget (x); both declare the same variable x. The same applies to function arguments so the code above looks like a declaration of a function that takes a first argument named x of type gadget and returns a widget...

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

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