(类型)值和类型(value)有什么区别?
什么区别
(type)value
C ++中有
type(value)
?
What is the difference between
(type)value
and
type(value)
in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
什么区别
(type)value
C ++中有
type(value)
?
What is the difference between
(type)value
and
type(value)
in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
没有区别;根据标准(§5.2.3):
由于该问题指定了
类型(value)
和(type)value
之间的差异,因此绝对没有区别。仅当您要处理值的逗号分隔列表时,才有差异。在这种情况下:
正如Troubadour指出的那样,
type(value)
版本的某些类型名称根本不会编译。例如:将编译,但是:
不会。具有不同名称的相同类型(例如,用
typedef
创建)可以工作:There is no difference; per the standard (§5.2.3):
Since the question specified the difference between
type(value)
and(type)value
, there is absolutely no difference.If and only if you're dealing with a comma-separated list of values can there be a difference. In this case:
As Troubadour pointed out, there are a certain names of types for which the
type(value)
version simply won't compile. For example:will compile, but:
will not. The same type with a different name (e.g., created with a
typedef
) can work though:没有区别; C ++标准(1998年和2003年版)很清楚这一点。尝试以下程序,请确保您使用合规的编译器,例如 http://comeaucomputing.com/ tryitout/。
注意:
unsigned(a_var)
是不同的,但确实显示了这些准确的令牌可能意味着其他的方式。它声明一个名为a_var
的变量, type nosigned,根本不是铸件。 (如果您熟悉功能或数组的指针,请考虑如何在p
中使用
。)void(*pf)()()()
或<p
代码> int(*pa)[42](发出警告,因为这些语句不使用该值,并且在实际程序中几乎可以肯定是错误的,但一切仍然有效。我只是使一切都排队后,没有心来改变它。)
There is no difference; the C++ standard (1998 and 2003 editions) is clear about this point. Try the following program, make sure you use a compiler that's compliant, such as the free preview at http://comeaucomputing.com/tryitout/.
Note:
unsigned(a_var)
is different, but does show one way those exact tokens can mean something else. It is declaring a variable nameda_var
of type unsigned, and isn't a cast at all. (If you're familiar with pointers to functions or arrays, consider how you have to use a parens aroundp
in a type likevoid (*pf)()
orint (*pa)[42]
.)(Warnings are produced since these statements don't use the value and in a real program that'd almost certainly be an error, but everything still works. I just didn't have the heart to change it after making everything line up.)
两者都是演员时没有区别,但是有时“类型(值)”不是演员。
以下是标准N3242草案,第8.2.1节的示例:
在这种情况下,“ int(a)”不是铸件,因为“ a”不是一个值,它是一个被冗余括号所包围的参数名称。文件指出
There is no difference when both are casts, but sometimes 'type(value)' is not a cast.
Here's an example from standard draft N3242, section 8.2.1:
In this case 'int(a)' is not a cast because 'a' is not a value, it is a parameter name surrounded by redundant parentheses. The document states
在C中,没有
类型(value)
,而在C/C ++中,type(value)
and(type)value
。In c there is no
type (value)
, while in c/c++ bothtype (value)
and(type) value
are allowed.为了在C ++中说明您的选择(只有一个安全检查)
To illustrate your options in C++ (only one has a safety check)