Domanda

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 ?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top