为模块化类的实例分配唯一的数字标识符 [英] Assigning Unique Numerical Identifiers to Instances of a Templated Class
问题描述
核心问题:
我想要能够接受一个模板类的实例,例如:
Core problem: I want to be able to take an instance of a templated class, say:
template<class a, class b, class c> class foo;
foo<int, float, double>;
然后执行以下操作:
foo<int, float, double>::value; //Evaluates to a unique number
foo<long, float, double>::value; //Evaluates to a different unique number
foo<int, float, double>::value; //Evaulates to the same unique number
除非,真的是:
template<class a, class b, class c>
int getUniqueIdentifier()
{
return foo<a, b, c>::value;
}
当前解决方案尝试:
我想使用Boost :: MPL的可扩展关联序列,因为每个元素都有它自己的唯一标识符,但我想我需要能够改变序列的位置,插入不做。 >
我可能正在咆哮错误的树。 (在正侧,dayum,但MPL!)
Current Solution Attempt:
I'm thinking I want to use Boost::MPL's "Extensible Associative Sequence", since each element gets it's own unique identifier, but I think I need to be able to alter the sequence in place, which "insert" doesn't do.
I may be barking up the wrong tree. (On the plus side, dayum, but MPL!)
目的:
重新发明信号插座系统。组件使用交换机来创建和注册信道,这将使用唯一标识符将信道放在映射中,从而允许运行时通用性。我尝试查找Qt库作为例子,但我不能解析他们的缩写,我想我缺少一些正式的技术诀窍。
Purpose:
Reinventing the wheel on a Signals & Sockets system. Components make and register channels with a "switchboard", which would use the unique identifiers to put the channels in a map, allowing run-time versatility. I've tried looking up the Qt library as an example, but I can't parse their abbreviations, and I think I'm missing some formal know-how.
谢谢!
推荐答案
如果你想把东西放在一个地图上,需要一个按类型的键,使用 std :: type_info :: before()
。可能值得派生一个类,以便您可以提供运算符<
,或者换行 std :: type_info :: before()
在二进制谓词中。
If you want to put things in a map, and need a per-type key, the proper solution is to use std::type_info::before()
. It may be worthwhile to derive a class so you can provide operator<
, alternatively wrap std::type_info::before()
in a binary predicate.
这篇关于为模块化类的实例分配唯一的数字标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!