编译时的C++类型id

Rob*_*ert 9 c++ hash typeid compile-time

我想在编译时根据派生类型为类生成哈希.今天我生成它像:

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

template<class Type>
const unsigned TypeBase<Type>::s_kID = hash(typeid(Type));
Run Code Online (Sandbox Code Playgroud)

但这会产生(非常不必要)运行时初始化代码(hash(..)函数根据std :: type_info :: name()执行简单的哈希)

想法?

Dav*_*nan 3

考虑到进程启动时发生的所有其他事情,以及现有代码的简单和优雅,假设您没有散列大量类型,我会保持现有解决方案原样。