我想在编译时根据派生类型为类生成哈希。今天我生成它如下:
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() 进行简单的散列)
想法?
最佳答案
考虑到进程启动时发生的所有其他事情,以及您现有代码的简单和优雅,假设您不对大量类型进行哈希处理,我将保留您现有的解决方案。
关于编译时的 C++ 类型标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7148877/