Cody Smith
Cody Smith

Reputation: 2732

Compare pointers by type?

How could I compare two pointers to see if they are of the same type?

Say I have...

int * a;
char * b;

I want to know whether or not these two pointers differ in type.

Details:

I'm working on a lookup table (implemented as a 2D void pointer array) to store pointers to structs of various types. I want to use one insert function that compares the pointer type given to the types stored in the first row of the table. If they match I want to add the pointer to the table in that column.

Basically I want to be able to store each incoming type into its own column.

enter image description here

Alternative methods of accomplishing this are welcomed.

Upvotes: 2

Views: 716

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

In this case, since you know the types before hand, it doesn't make much sense to check. You can just proceed knowing that they are different types.

However, assuming that perhaps the types may be dependent on some compile-time properties like template arguments, you could use std::is_same:

std::is_same<decltype(a), decltype(b)>::value

This will be true if they are the same type and false otherwise.

Upvotes: 9

Related Questions