Robert
Robert

Reputation: 2389

C++ type id at compile time

I want to generate a hash for a class based on its derived type at compile time. Today I generate it like:

template<class Type>
class TypeBase 
{
public:
    static const unsigned s_kID;
};

template<class Type>
const unsigned TypeBase<Type>::s_kID = hash(typeid(Type));

but this generates (pretty unnecessarily) run time initialization code (the hash(..) function does a simple hash based on std::type_info::name() )

Ideas ?

Upvotes: 10

Views: 3420

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

Given everything else that happens at process startup, and how simple and elegant your existing code is, assuming you don't hash a gazillion types, I'd leave your existing solution exactly as it is.

Upvotes: 3

Related Questions