简体   繁体   English

C++ 如果我知道几个可能的确切类型,我应该使用什么类型?

[英]C++ what type should I use if I know the exact type from a few possible?

hope you'll help me to understand.希望你能帮助我理解。 I have a class, that defines a buffer:我有一个 class,它定义了一个缓冲区:

typedef std::variant<signed char, unsigned char, short, unsigned short, int, unsigned int, float> BufferData;

struct BufferAttribute {
  unsigned int index;
  int size = 4;
  char format = ATTRIBUTE_FORMAT::FLOAT;
  bool normalized = false;
  int stride = 0;
  int offset = 0;
  std::vector<BufferData> data;
  std::string name;
};

As you can see, I have a char that defines a format, so in the future, I will know the exact data format.如您所见,我有一个定义格式的char ,所以将来我会知道确切的数据格式。 But as for the data, I'm using a variant which makes me worry about the performance of the visitor.但至于数据,我使用了一个变体,这让我担心访问者的表现。 What type can I use to make it faster or better?我可以使用什么类型使它更快或更好? Or do you think that Variant is a good choice?还是您认为Variant是一个不错的选择?


PS I worrying about processing thousands of numbers from this buffer in real-time in my game app. PS 我担心在我的游戏应用程序中实时处理来自这个缓冲区的数千个数字。

If you are concerned about performance, you have to measure it in your use case.如果您担心性能,则必须在用例中对其进行衡量。 General statements wrt.一般陈述performance tend to be wrong or may out date with the next compiler version and or hardware generation.性能往往是错误的,或者可能会随着下一个编译器版本和/或硬件一代而过时。

Here the general performance of std::variant has been analyzed. 这里已经分析了 std::variant 的一般性能。

If this is applicable to you is in question.如果这适用于您是有问题的。 You have to make sure that the ´std::variant´ in BufferAttribute actually is your hotspot.您必须确保BufferAttribute中的“std::variant”实际上是您的热点。 You can find the hotspots using a tool such as perf or vTune .您可以使用perfvTune等工具找到热点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM