Home > Back-end >  Comparing same types with different names
Comparing same types with different names

Time:11-10

I've got

typedef void* TA;
typedef void* TB;

I need to compare names of types as different types. There are std::is_same<TA, TB>::value and typeid(TA)==typeid(TB) return true, but I need false.

CodePudding user response:

That is not possible because they are the same type.

If you are trying to use these as opaque handles with strong type-checking, another thing you can do is to forward-declare the classes in a header, and define them in your implementation file:

// ta_tb.hpp

typedef struct ta_impl* TA;
typedef struct tb_impl* TB;

Later:

// ta_tb.cpp

struct ta_impl { /* ... */ };
struct tb_impl { /* ... */ };

This enables type-checking for the handles, but doesn't leak the implementation details.

  •  Tags:  
  • c
  • Related