使用函数地址的模板类的唯一数字 ID
所以,这个问题之前已经被问过,但我想要一个标题中包含一些关键词的问题。
问题很简单:如何才能拥有一个模板类,以便对于模板的每个实例(而不是类的每个实例)都有一个唯一的数字标识符?
也就是说,一种区分方法:
foo<int> f1;
foo<char> f2;
classID(f1) != classID(f2);
但是,
foo<int> f3;
foo<int> f4;
classID(f3) == classID(f4);
相关于:
So, this question has been asked before, but I wanted a question with some of those key words in the title.
The issue is simple: How can I have a templated class, such that for each instance of the template - but not each instance of the class - there is a unique, numerical identifier?
That is, a way to differentiate:
foo<int> f1;
foo<char> f2;
classID(f1) != classID(f2);
but,
foo<int> f3;
foo<int> f4;
classID(f3) == classID(f4);
Related to:
in C++, how to use a singleton to ensure that each class has a unique integral ID?
Assigning Unique Numerical Identifiers to Instances of a Templated Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就是这样!它是轻量级的,超级简单,并且不使用RTTI,尽管它使用了极其不安全的reinterpret_cast。
虽然,也许我错过了什么?
That's how! It's lightweight, super easy, and doesn't use RTTI, although it uses the ridiculously unsafe reinterpret_cast.
Although, maybe I'm missing something?
我认为你可以为此使用一个静态函数,并继承它的类:
当然,这不会是一个静态编译时常量 - 我认为这是不可能自动实现的(你必须手动将这些类型添加到一些类型列表,例如 mpl::vector )。请注意,如果只是比较类型的相等性,则不需要所有这些。您只需要使用
is_same
(在 boost 和其他库中找到并且编写起来很简单),它会产生一个编译时间常数I think you can just use a static function for this, and inherit its class:
Of course, that won't be a static compile time constant - i don't think that is possible automatically (you would have to add those types manually to some type list like
mpl::vector
). Please notice that for just comparing types for equality, you don't need all this. You just need to useis_same
(found in boost and other libraries and trivial to write) which yields a compile time constant