Click here to Skip to main content
15,850,685 members
Articles / Programming Languages / C++
Tip/Trick

Quickly check whether C++ template instances have the same parameters

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
5 Feb 2012CPOL 41.5K   4   11
How to check whether two template instances of a C++ class have the same template parameters without using dynamic_cast

Imagine the following C++ class structure (code is reduced):


C++
class BaseClass {
public:
  virtual bool equals(BaseClass* other) = 0;
};

template<T>
class ChildClass {
public:
  virtual bool equals(BaseClass* other) {
    // How to check whether this cast is ok?
    ChildClass<T> otherCast = static_cast<ChildClass<T>>(other);
  }
};

How to check whether the static_cast is valid? One way is to use dynamic_cast.


Here's an alternative if you don't want/can't use dynamic_cast, implemented through classID():


C++
class BaseClass {
public:
  virtual bool equals(BaseClass* other) = 0;
  virtual int classID() const = 0;
};

template<class T>
class ChildClass {
public:
  virtual bool equals(BaseClass* other) {
    if (this->classID() != other->classID()) {
      return false;
    }
    ChildClass<T> otherCast = static_cast<ChildClass<T>>(other);
  }

  virtual int classID() const {
    static int id; 
    return (int)&id; // generates a custom "id"
  }
};

Each template instantiation of ChildClass will have another ID. So, for example, ChildClass<MyClass> will have another ID as ChildClass<MyOtherClass>. Using the same template parameter(s) will result in the same ID.


Note: Although I've tested this only with Visual C++ 2010, it seems logical that this will work with other compilers too. I haven't tested this across DLL boundaries.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer University of Stuttgart
Germany Germany
I have studied Software Engineering and am currently working at the University of Stuttgart, Germany.

I have been programming for many years and have a background in C++, C#, Java, Python and web languages (HTML, CSS, JavaScript).

Comments and Discussions

 
GeneralThanks for reply. 1.You are right about virtual methods. No... Pin
geoyar10-Feb-12 9:30
professionalgeoyar10-Feb-12 9:30 
General1. You do not want to use run time type info (dynamic_cast),... Pin
geoyar9-Feb-12 16:39
professionalgeoyar9-Feb-12 16:39 
GeneralRe: 1. RTTI has nothing to do with virtual methods. 2. It's just... Pin
Sebastian Krysmanski9-Feb-12 23:37
Sebastian Krysmanski9-Feb-12 23:37 
GeneralI should have written that this is supposed to be a very fas... Pin
Sebastian Krysmanski7-Feb-12 22:09
Sebastian Krysmanski7-Feb-12 22:09 
GeneralAs the tip is currently written, the ChildClass<T>::equals m... Pin
Doc Lobster7-Feb-12 6:11
Doc Lobster7-Feb-12 6:11 
GeneralNone of your functions are const (and equals() and Id() defi... Pin
Dominic Amann7-Feb-12 4:11
Dominic Amann7-Feb-12 4:11 
Generalmartin, geoyar, xryl669 are perfectly correct. This is not g... Pin
Lakamraju Raghuram6-Feb-12 23:48
Lakamraju Raghuram6-Feb-12 23:48 
GeneralYou're reinventing typeid typeof and the rest, but worst, yo... Pin
xryl6695-Feb-12 23:42
xryl6695-Feb-12 23:42 
You're reinventing typeid typeof and the rest, but worst, you're making all the common error of a naive implementation. If you either derive from a ChildClass, you might get the same classID (because you might forget to overload the method).
There is no persistance either in the id (meaning that from one run of the software to another run you'll get different results), and you can't map back to the class name to figure out the id.
You're also paying the cost of adding/growing a virtual table, while typeid doesn't (since it's compile-time based indentification).
GeneralReason for my vote of 5 Good one,my 5. Boost meta programmin... Pin
Ashish Tyagi 4030-Jan-12 17:14
Ashish Tyagi 4030-Jan-12 17:14 
GeneralI do not think it is safe and good. All code is based on ass... Pin
geoyar30-Jan-12 16:47
professionalgeoyar30-Jan-12 16:47 
GeneralAs a matter of fact, it doesn't work across DLL boundaries. ... Pin
martin_bisson30-Jan-12 11:38
martin_bisson30-Jan-12 11:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.