类型安全和reinterpret_cast<> [英] type safety and reinterpret_cast<>

查看:97
本文介绍了类型安全和reinterpret_cast<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们采取了哪些措施来确保在处理C API

回调函数时你做了相应的reinterpret_cast<>?对于

实例,今天我遇到了错误类型是演员阵营的

目标的情况。当然有了reinterpret_cast,在UB咬你的屁股之前没有任何抱怨

。在我看来,应该是一种处理这些功能的方法,但仍保留一些类型安全的外观。也许升级

中的任何一个或变体都会有帮助吗?


你们做些什么来防止你自己踩到这些

情况?

What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong type was the
target of a cast. Of course with a reinterpret_cast nothing complains
until the UB bites you in the ass. It seems to me that there ought to
be a way to deal with these kinds of functions yet still retain some
semblance of type safety. Perhaps either any or variant from boost
would help?

What do you guys do to keep from stabbing your own foot in these
situations?

推荐答案

" Noah Roberts" < ro ********** @ gmail.com写信息

新闻:11 ******************** *@m73g2000cwd.googlegro ups.com ...
"Noah Roberts" <ro**********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

人们采​​取什么措施来确保在处理C API时

回调你做了相应的函数reinterpret_cast<>?对于

实例,今天我遇到了错误类型是演员阵营的

目标的情况。当然有了reinterpret_cast,在UB咬你的屁股之前没有任何抱怨

。在我看来,应该是一种处理这些功能的方法,但仍保留一些类型安全的外观。也许升级

中的任何一个或变体都会有帮助吗?


你们做些什么来防止你自己踩到这些

情况?
What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong type was the
target of a cast. Of course with a reinterpret_cast nothing complains
until the UB bites you in the ass. It seems to me that there ought to
be a way to deal with these kinds of functions yet still retain some
semblance of type safety. Perhaps either any or variant from boost
would help?

What do you guys do to keep from stabbing your own foot in these
situations?



如果你使用reinterpret_cast,你最好知道你在做什么,或者

不要这样做。 reinterpret_cast是最危险的演员,只有绝对必要时才应该使用

。如果可能的话,使用static_cast有点

mroe更安全。


除此之外,如果你使用它错了你在脚下射击yoru self。 />

If you use reinterpret_cast you better know what the heck you''re doing, or
don''t do it. reinterpret_cast is the most dangerous cast and should be used
only when absolutly necessary. Use static_cast if possible which is a bit
mroe safer.

Other that that, if you use it wrong you shoot yoru self in the foot.


Noah Roberts写道:
Noah Roberts wrote:

人们采​​取什么措施来确保在处理C API时

你做适当的回调函数reinterpret_cast<>?对于

实例,今天我遇到了错误类型是演员阵营的

目标的情况。当然有了reinterpret_cast,在UB咬你的屁股之前没有任何抱怨

。在我看来,应该是一种处理这些功能的方法,但仍保留一些类型安全的外观。也许升级

中的任何一个或变体都会有帮助吗?


你们做些什么来防止你自己踩到这些

情况?
What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong type was the
target of a cast. Of course with a reinterpret_cast nothing complains
until the UB bites you in the ass. It seems to me that there ought to
be a way to deal with these kinds of functions yet still retain some
semblance of type safety. Perhaps either any or variant from boost
would help?

What do you guys do to keep from stabbing your own foot in these
situations?



reinterpret_cast的全部含义是请编译,退出

的方式并做我告诉你的事情,那些你有这些东西,他们是这种类型的b $ b,只是这样做。


我通常将重新解释演员表重新用于非常小的用法我

可以很容易地在视觉上检查我知道发生了什么。


你可以做的一件事是使用一种体现某种类型的类型
$ b每当与C api交谈时,$ b安全 - 也许使用任何

包含所需指针的类。这样你只能期望一种类型来自C回调来回来回
你可以验证他们

包含正确的类型。然而,你如何管理自己的一生呢

另一个故事。

The whole meaning of reinterpret_cast is "please compiler, get out of
the way and do what I tell you to do, those bits you have, they''re of
this type and just do it".

I usually relegate the reinterpret casts to very minimal usage where I
can check visually very easily that I know is happening.

One thing you could do is use a single type that embodies some type
safety whenever talking to the C api - perhaps use an "Any" class that
contains the desired pointer. That way you only expect one type to come
back and forth from the C call backs and you can verify that they
contain the right type. How you manage their lifetime however is
another story.




Jim Langston写道:

Jim Langston wrote:

" Noah Roberts" < ro ********** @ gmail.com写信息

新闻:11 ******************** *@m73g2000cwd.googlegro ups.com ...
"Noah Roberts" <ro**********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

人们采​​取什么措施来确保在处理C API时

回调你做了相应的函数reinterpret_cast<>?对于

实例,今天我遇到了错误类型是演员阵营的

目标的情况。当然有了reinterpret_cast,在UB咬你的屁股之前没有任何抱怨

。在我看来,应该是一种处理这些功能的方法,但仍保留一些类型安全的外观。也许升级

中的任何一个或变体都会有帮助吗?


你们做些什么来防止你自己踩到这些

情况?
What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong type was the
target of a cast. Of course with a reinterpret_cast nothing complains
until the UB bites you in the ass. It seems to me that there ought to
be a way to deal with these kinds of functions yet still retain some
semblance of type safety. Perhaps either any or variant from boost
would help?

What do you guys do to keep from stabbing your own foot in these
situations?



如果你使用reinterpret_cast,你最好知道你在做什么,或者

不要这样做。 reinterpret_cast是最危险的演员,只有绝对必要时才应该使用

。如果可能的话,使用static_cast有点

mroe更安全。


If you use reinterpret_cast you better know what the heck you''re doing, or
don''t do it. reinterpret_cast is the most dangerous cast and should be used
only when absolutly necessary. Use static_cast if possible which is a bit
mroe safer.



嘿,真的吗?

Heh, really?


>

除此之外,如果你使用它错了你在脚下射击yoru self。
>
Other that that, if you use it wrong you shoot yoru self in the foot.



非常有洞察力。

very insightfull.


这篇关于类型安全和reinterpret_cast&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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