看起来像函数调用的强制转换? [英] Casts that look like function calls?

查看:74
本文介绍了看起来像函数调用的强制转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C ++主要来自边做边学。我的主要参考是

Stoustrup的书。我对同事的代码感到困惑,

看起来像这样:


abc = BOOL(def)

我问他那是什么,他说这是演员。我知道所有关于

dynamic_cast和朋友,但这对我来说是新鲜事。总而言之,我的两个同事似乎都熟悉

写一个演员就好像它是一个函数,尽管它们通常不是

以这种方式编写它们,而Stoustrup的书中有一些实际的实例,这些实例出现在他的一些代码示例中。一位同事能够在正式的C ++语法描述中找到

的东西,表明它是有效的......

但没有人能够指出我的任何文档,描述,或

说明。


如果在Stroustrup的书中有描述,我找不到它。我认为不赞成使用C风格的演员。可能是它,但是,不,那只是

常规C cast语法,我已经验证C编译器会_not_

接受写一个演员表好像它是一个功能。


它也不是微软主义,这是我的另一个想法;其他

C ++编译器似乎接受它。


那么,到底是什么?它是完全合法的C ++吗?它是否与正常有任何差异?
差异施放,即做


hij = long(klm);



hij =(long)klm;


编译相同的代码?如果是这样,它有什么用?是否只是为了将C语言扩展到C ++而需要做的事情的意外结果?在Stoustrup的书中究竟是什么描述了

并解释了?

解决方案

DP **************** *****@gmail.com schrieb:


我知道C ++主要来自边做边学。我的主要参考是

Stoustrup的书。我对同事的代码中的某些东西感到困惑,

看起来像这样:


abc = BOOL(def)


这是正常的构造函数语法。假设BOOL是一个类型而不是

a函数名,上面的行构造了一个BOOL类型的变量来自

def,这与从def到BOOL的强制转换不同。如果BOOL是一个

类或结构,那么代码只会在BOOL有一个构造函数

时才会编译,该构造函数采用def'类型的参数。如果BOOL是

基元类型的typedef,则如果def可转换为

类型,代码将编译。只有在后一种情况下上面的线才能与演员相媲美。


Ralpe




ralpe ha escrito:

dp ********************* @ gmail.com schrieb:


我知道C ++主要来自边做边学。我的主要参考是

Stoustrup的书。我对同事的代码中的某些东西感到困惑,

看起来像这样:


abc = BOOL(def)



对我来说,它被定义为一个执行简单演员的宏,应该是

定义为:


#define BOOL(x)(bool)x


你可以看到,每次调用BOOL(x)宏时,预处理器

将它转换为(bool)x。


我没有看到宏用法有用,这使得代码很难阅读和理解。


ralpe< ra ************ @ gmx.netwrote:

dp *********** **********@gmail.com schrieb:


>我知道C ++主要来自边做边学。 ;我的主要参考是Stoustrup的书。

abc = BOOL(def)


$ b $我对同事代码中的某些内容感到困惑:

b这是普通的构造函数语法。假设BOOL是一个类型而不是

a函数名,上面的行构造了一个BOOL类型的变量来自

def,这与从def到BOOL的强制转换不同。如果BOOL是一个

类或结构,那么代码只会在BOOL有一个构造函数

时才会编译,该构造函数采用def'类型的参数。如果BOOL是

基元类型的typedef,则如果def可转换为

类型,代码将编译。仅在后一种情况下,上述线与铸造相当。



此外,函数式强制转换不适用于其中包含空格

的类型(例如,unsigned long; )。


-

Marcus Kwok

用''net''替换''invalid''回复


I know C++ mostly from "learning by doing." My main reference is
Stoustrup''s book. I was puzzled by something in a colleague''s code that
looked like this:

abc=BOOL(def)

I asked him what that was, and he said "it''s a cast." I know all about
dynamic_cast and friends, but this was something new to me. To make a
long story short, both of my colleagues seemed to be familiar with
writing a cast as if it were a function, although they don''t normally
write them that way, and Stoustrup''s book has actual instances of this
that appear in some of his code examples. A colleague was able to find
something in the formal C++ syntax description indicating it''s valid...
but nobody was able to point me to any documentation, description, or
explanation.

If this is described in Stroustrup''s book, I couldn''t find it. I
thought "deprecated C-style cast" might be it, but, no, that''s just the
regular C cast syntax, and I''ve verified that C compilers do _not_
accept writing a cast as if it were a function.

It is not a Microsoft-ism, either, which was my other thought; other
C++ compilers seem to accept it.

So, what the heck is it? Is it fully legitimate C++? Does it have any
differences whatsoever from a "normal" cast, i.e. do

hij = long(klm);
and
hij = (long) klm;

compile identical code? If so, what is it there for? Is it just an
unintended consequence of things that needed to be done to extend C
syntax to C++? And where exactly in Stoustrup''s book is it described
and explained?

解决方案

dp*********************@gmail.com schrieb:

I know C++ mostly from "learning by doing." My main reference is
Stoustrup''s book. I was puzzled by something in a colleague''s code that
looked like this:

abc=BOOL(def)

This is normal constructor syntax. Assuming that BOOL is a type and not
a function name the above line constructs a variable of type BOOL from
def, which is not the same as a cast from def to BOOL. If BOOL is a
class or struct the code will only compile if BOOL has a constructor
that takes an argument of def''s type. If BOOL is a typedef for a
primitive type the code will compile if def is convertable to that
type. Only in the latter case is the above line comparable to a cast.

Ralpe



ralpe ha escrito:

dp*********************@gmail.com schrieb:

I know C++ mostly from "learning by doing." My main reference is
Stoustrup''s book. I was puzzled by something in a colleague''s code that
looked like this:

abc=BOOL(def)

To me, it is defined as a macro that does the simple cast and should be
defined as:

#define BOOL(x) (bool) x

as you can see, everytime the BOOL(x) macro is called, the preprocessor
will transform it onto (bool) x.

I do not see the macro usage useful and it makes the code very hard to
read and understand. The


ralpe <ra************@gmx.netwrote:

dp*********************@gmail.com schrieb:

>I know C++ mostly from "learning by doing." My main reference is
Stoustrup''s book. I was puzzled by something in a colleague''s code that
looked like this:

abc=BOOL(def)


This is normal constructor syntax. Assuming that BOOL is a type and not
a function name the above line constructs a variable of type BOOL from
def, which is not the same as a cast from def to BOOL. If BOOL is a
class or struct the code will only compile if BOOL has a constructor
that takes an argument of def''s type. If BOOL is a typedef for a
primitive type the code will compile if def is convertable to that
type. Only in the latter case is the above line comparable to a cast.

Also, the function-style cast will not work for types that have a space
in them (e.g., "unsigned long").

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


这篇关于看起来像函数调用的强制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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