简体   繁体   English

如何使用预处理器指令比较两种类型是否相等?

[英]How can I compare if two types are equal using preprocessor directives?

I have a class that allows for dynamically setting specific values.我有一个允许动态设置特定值的类。 It has multiple constructors in the following way:它有多个构造函数,如下所示:

class Property {
  enum PropertyType { INT32, UINT32, UINT64, OBJECTSIZE, ... };
public:
  Property(int32_t value_) : type(INT32), value(value_) {}
  Property(uint32_t value_) : type(UINT32), value(value_) {}
  Property(uint64_t value_) : type(UINT64), value(value_) {}
  Property(size_t value_) : type(OBJECTSIZE), value(value_) {}

private:
  std::any value;
  PropertyType type;
};

This works really well except for one part.除了一部分之外,这非常有效。 Depending on compiler, size_t value can be a different type.根据编译器的不同, size_t值可以是不同的类型。 For example, on macOS with clang, size_t is a custom type while on Linux with GCC, size_t is uint64_t .例如,在带有 clang 的 macOS 上, size_t是一个自定义类型,而在带有 GCC 的 Linux 上, size_tuint64_t As a result, I am getting compiler error due to conflicting types between two constructors.结果,由于两个构造函数之间的类型冲突,我收到编译器错误。

Is there a way that I can add a preprocessor condition to disable size_t constructor if the types match?如果类型匹配,有没有办法添加预处理器条件以禁用size_t构造函数? Something like:就像是:

#if uint64_t != size_t or uint32_t != size_t
  Property(size_t value_) : type(OBJECTSIZE), value(value_) {}
#endif

How can I compare if two types are equal using preprocessor directives?如何使用预处理器指令比较两种类型是否相等?

You can't - preprocessor is not aware of types.你不能 - 预处理器不知道类型。

This works really well except for one part.除了一部分之外,这非常有效。

Use SFINAE to disable the overload when size_t is the same as the others.size_t与其他相同时,使用 SFINAE 禁用重载。

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

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