0

fast什么是C++ 中通用数据类型的最佳方法。我有一个事件系统,需要能够在程序周围传递事件信息。

sendEvent(123, "a String", aFloat, aVec3, etc); // sendEvent(EventID, EventArg, EventArg ...);

eventData.at(0).asFloat();
eventData.at(1).asInt(); // etc.

到目前为止,我只需要 float/int/ptr,我在联合中处理了这个。但现在我有一些大多数结构,这变得有点复杂,所以我正在重新审视它。

我进行了深入研究,boost::any但这太慢了,可能会有很多数据飞来飞去,但这个想法似乎是正确的方向。

我对使用 void* 数据保持有一个相当幼稚的想法,但很快就变得非常难看。

4

1 回答 1

1

如果您的支持类型有限,您可以尝试boost::variant. 这或多或少是一个更好的联盟。它避免了boost::any正在使用的堆分配,并且应该提供最大的性能。

http://www.boost.org/doc/libs/1_56_0/doc/html/variant.html

例子:

// create a variant capable of storing int, float and string
boost::variant<int, float, std::string> eventData;
eventData = 42.f; // store a float
float f = boost::get<float>(eventData); // get the float
于 2014-09-06T14:46:30.003 回答