Gasim
Gasim

Reputation: 8001

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

Upvotes: 2

Views: 1040

Answers (1)

KamilCuk
KamilCuk

Reputation: 142005

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.

Upvotes: 1

Related Questions