0

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.

  • 2
    That won't work because you can just replace `TA` with `void*` anywhere you use it. A `typedef` does not create a new type, it only creates an alias – UnholySheep Nov 09 '21 at 16:05
  • 3
    `typedef` and `using` don't define new types, they create aliases. `TA` and `TB` are the same type. To create a new type you need `struct` or `class` (or I guess `union`). For example `struct TA { void * ptr; };`. – François Andrieux Nov 09 '21 at 16:06
  • 1
    fyi or duplicate (not sure) https://stackoverflow.com/questions/47512925/strong-typedef-for-primitive-types-boost-strong-typedef-is-not-cutting-it – Richard Critten Nov 09 '21 at 16:27

1 Answers1

0

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93