65.9K
CodeProject is changing. Read more.
Home

Quickly Check whether C++ Template Instances have the Same Parameters

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 7, 2012

CPOL
viewsIcon

8463

This post helps to quickly check whether C++ template instances have the same parameters.

Setting up a custom type identification mechanism only makes sense if RTTI is not available or you cannot guarantee that it is consistent. That might be the case on some platforms with dynamic libraries.

A simple static_cast will not work for all possible inheritance paths. Also, your method generates a different id in each translation unit ChildClass<t> is pulled into. I have yet to see a solution for "safe" class IDs mechanism fundamentally different from that used by COM.

So if you have RTTI, you just do this:

virtual bool equals(BaseClass *other) const
{
    if (auto temp = dynamic_cast<ChildClass<T>*>(other)) {
        //...
    }
}