编译时的c++类型id

C++ type id at compile time

本文关键字:类型 id c++ 编译      更新时间:2023-10-16

我想在编译时根据类的派生类型生成一个哈希值。今天我像这样生成它:

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

但这会生成(相当不必要的)运行时初始化代码(hash(..)函数基于std::type_info::name()进行简单的散列)

想法?

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