编译时的C ++类型id

时间:2011-08-22 14:19:03

标签: c++ hash compile-time typeid

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

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()执行简单的哈希)

想法?

1 个答案:

答案 0 :(得分:3)

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

相关问题