Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 284
Robert Avatar asked Aug 22 '11 14:08

Robert


1 Answers

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.

like image 173
David Heffernan Avatar answered Sep 30 '22 11:09

David Heffernan