简体   繁体   中英

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. For example, on macOS with clang, size_t is a custom type while on Linux with GCC, size_t is uint64_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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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