static_cast真的和C / C ++样式一样快吗? [英] Is static_cast really as fast as C/C++ style casts?

查看:89
本文介绍了static_cast真的和C / C ++样式一样快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。简而言之,之间是否有任何性能差异:


float f = 10.0f;

int i = static_cast< int>(f);





float f = 10.0f;

int i = int(f);


我想问这个问题一段时间但是有多快

static_cast?我一直假设(没有证明)static_cast是作为模板实现的,只是包装了一个C / C ++风格的演员。然后

它是Josuttis的书,它说:


只有在定义了类型转换时才允许转换


大概这个测试是在编译时完成的吗? />

另外,如果static_cast只是C / C ++演员的包装器,那么<​​br />
与reinterpret_cast的区别是什么?也许这些东西都是编译器

依赖,但任何信息都很有用。


谢谢,


David

Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int>(f);

and

float f = 10.0f;
int i = int(f);

I''ve been meaning to ask this for a while but just how fast is
static_cast? I had always assumed (without proof) that static_cast is
implemented as a template which just wraps a C/C++ style cast.But then
it the Josuttis book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?

Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cast? Maybe these things are compiler
dependant but any info is useful.

Thanks,

David

推荐答案


大家好。简而言之,之间是否有任何性能差异:


float f = 10.0f;

int i = static_cast< int>(f);





float f = 10.0f;

int i = int(f);
Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int>(f);

and

float f = 10.0f;
int i = int(f);



否,因为强制转换是一个编译时的概念 - 不是运行时概念。

两者都应该生成相同的机器代码。


No, because a "cast" is a compile-time concept -- not a runtime concept.
Both should produce identical machine code.


我'我一直想问这个问题,但是有多快这个问题

static_cast?
I''ve been meaning to ask this for a while but just how fast is
static_cast?



它没有速度。


It doesn''t have a speed.


我一直认为(没有证明)static_cast实现为

a模板,它只包含一个C / C ++风格的演员。然后它就是Josuttis

的书,它说:


"只有在定义了类型转换时才允许转换


大概这个测试是在编译时完成的吗?
I had always assumed (without proof) that static_cast is implemented as
a template which just wraps a C/C++ style cast.But then it the Josuttis
book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?



是的。使用符合标准的编译器进行测试:


char * p = 0;


long i = static_cast< long>(p);


你应该得到编译器错误。


Yes it is. Test it with a conforming compiler:

char *p = 0;

long i = static_cast<long>(p);

You should get a compiler error.


另外,如果static_cast只是C / C ++强制转换的包装,那么

它与reinterpret_cast有什么不同?也许这些东西都是编译器

依赖,但任何信息都很有用。
Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cast? Maybe these things are compiler
dependant but any info is useful.



我在上面给你看的方式。有一些东西列出了static_cast

会让你做的事情,还有其他的东西,它不会让你这么做。对于它不会让你这么做的事情,你必须使用reinterpret_cast。


-


Frederick Gotham


In the way I showed you above. There''s a list of things which static_cast
WILL let you do, and others which it WON''T let you do. For the things it
won''t let you do, you must use reinterpret_cast.

--

Frederick Gotham


es ***** @ googlemail.com 写道:
es*****@googlemail.com wrote :

据推测,这个测试是在编译时完成的吗?
Presumably then this test is done at compile time?



是。

Yes.


>
>


好的,感谢输入。我很想写出正确的代码,但是如果可能的话,我不想要牺牲速度。好像我在这里好:-D

es ***** @ googlemail.com 写道:
Ok, thanks for the input. I''m keen to write correct code but don''t want
to sacrifice speed if possible. Seems like I''m ok here :-D

es*****@googlemail.com wrote:

大家好。简而言之,之间是否有任何性能差异:


float f = 10.0f;

int i = static_cast< int>(f);





float f = 10.0f;

int i = int(f);


我想问这个问题一段时间但是有多快

static_cast?我一直假设(没有证明)static_cast是作为模板实现的,只是包装了一个C / C ++风格的演员。然后

它是Josuttis的书,它说:


只有在定义了类型转换时才允许转换


大概这个测试是在编译时完成的吗? />

另外,如果static_cast只是C / C ++演员的包装器,那么<​​br />
与reinterpret_cast的区别是什么?也许这些东西都是编译器

依赖但任何信息都很有用。


谢谢,


David
Hi all. In short, is there any performance difference between:

float f = 10.0f;
int i = static_cast<int>(f);

and

float f = 10.0f;
int i = int(f);

I''ve been meaning to ask this for a while but just how fast is
static_cast? I had always assumed (without proof) that static_cast is
implemented as a template which just wraps a C/C++ style cast.But then
it the Josuttis book it says:

"The conversion is allowed only if a type conversion is defined"

Presumably then this test is done at compile time?

Also, if static_cast is just a wrapper around C/C++ casts then how does
it differ from reinterpret_cast? Maybe these things are compiler
dependant but any info is useful.

Thanks,

David


这篇关于static_cast真的和C / C ++样式一样快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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