C++ 中可选的结构类型可能性或任何其他语言?
在 C++ 中如何告诉编译器 Ogre::Vector3 IS_SAME_AS SomeOtherLIB::Vector3 ? 我觉得……在像 c++ 这样的语言中,它们不是结构类型的,但在某些情况下它是有意义的。
通常,作为游戏开发人员,在使用 4 个以上提供排序或自己的 Vector3 实现的库时。代码中充斥着 ToOgre、ToThis、ToThat 转换函数。这是大量的 Float3 复制,不应该首先发生。
在 C++ 或任何其他语言中,我们不必从一种类型转换(复制)到另一种类型,这本质上是相同的。但是 C++ 中的任何解决方案都适用于 C/C++,因为大多数优秀的游戏开发库都是针对 C/C++ 的。
In C++ how to tell compiler that Ogre::Vector3 IS_SAME_AS SomeOtherLIB::Vector3 ?
I feel that.. in languages like c++ which are not structural typed but there are cases when it makes sense.
Normally as game developer when working with 4+ libraries that provide sort or their own Vector3 implementation. The code is littered with ToOgre, ToThis, ToThat conversion function. Thats a lot of Float3 copying around which should not happen on first place.
Is in C++ or any other languages where we dont have to convert (copying) from one type to another which is essentially the samething. But any solution in C++ as most of the good gamedevs libs are for c/c++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果使用模板,则可以定义采用任何类型参数的函数,只要在该类型上定义了必要的操作即可。例子:
If you use templates you can define functions that take any type of argument as long as the necessary operations are defined on that type. Example:
虽然它不适合任何情况,但模板可以为您提供“编译时鸭子类型”。
假设您有两种向量类型:
您可以定义函数模板来隐藏如何获取组件的实现:
使用此类帮助程序,您现在可以编写适用于这两种向量的通用函数:
您还可以继续专门化实用程序,例如
length ()
出于性能或其他原因,例如,如果某个向量已经有一个为您提供长度的成员函数:While it doesn't suit any situation, templates can give you "compile-time duck typing".
Lets say you have two vector types:
You can define function templates that hide the implementation how of to get the components:
With such helpers you can now write generic functions that work on both:
You can also continue by specializing utilities like
length()
for performance or other reasons, e.g. if a certain vector already has a member function providing you with the length:如果您确实确定是非虚拟结构,则可以执行reinterpret_cast。但是,最好:
If you are really sure in case of non-virtual structures, you can do a reinterpret_cast. However, it's better to:
Haxe 是一种高度可移植的语言,具有完全可选的结构子类型:
Vector3 不仅是一个已经可用的结构,而且还充当结构其他类的接口。这样的 typedef 结构可以指定函数签名和字段。
Haxe 还有一个用于与 C++ 对话的 CFFI(尽管它仍然需要转换方法),并且已经存在一些 C++ 游戏引擎以及各种较低级别框架的绑定。用纯 Haxe 编写的跨平台引擎也在开发中,针对不同的 C++、Flash 和 JS(Canvas 和 WebGL)。
这可能不是您现在正在寻找的解决方案,但几年后可能会变得更有趣。
Haxe is a highly portable language with completely optional structural subtyping:
Not only is Vector3 an already usable structure, it also acts as a structural interface for other classes. Such
typedef
'd structs can specify function signatures as well as fields.Haxe also has a CFFI for talking to C++ (although it still requires conversion methods), and bindings already exist for a few C++ game engines, as well as a variety of lower-level frameworks. Cross-platform engines written in pure Haxe are also being developed, targeting variously C++, Flash, and JS (both Canvas and WebGL).
This is probably not the solution you are looking for right now, but may become more interesting within a few years.