C++在编译时输入 id
我想在编译时根据其派生类型为类生成哈希。今天我生成它是这样的:
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() 进行简单的哈希)
想法?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑到进程启动时发生的所有其他事情,以及现有代码的简单和优雅,假设您没有散列大量类型,我会保持现有解决方案原样。
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.