编译时常数id [英] Compile-time constant id

查看:216
本文介绍了编译时常数id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下:

 模板< typename T> 
class A
{
public:
static const unsigned int ID =?
};

我想让ID为每个T生成一个唯一的编译时间ID。 $ c> __ COUNTER __ 和boost PP库,但迄今为止都不成功。


$ b

编辑:ID必须可以在switch语句中使用。


$ b 编辑2:所有基于静态方法或成员的地址的答案都不正确。虽然他们创建一个唯一的ID,但是它们在编译时不能解析,因此不能用作switch语句的情况。

解决方案

这是足够的假设一个符合标准的编译器(相对于一个定义规则):

  template< typename T& 
class A
{
public:
static char ID_storage;
static const void * const ID;
};

template< typename T> char A< T> :: ID_storage;
template< typename T> const void * const A< T> :: ID =& A T :: ID_storage;

从C ++标准3.2.5一个定义规则[basic.def.odr] ):


...如果D是一个模板并且在多个翻译
单位中定义,上面的列表中的要求应该将
应用于模板
定义(14.6.3)中使用的模板包含范围中的名称,以及
实例化时的依赖名称(14.6。 2)。 如果D的定义满足所有这些
要求,那么程序将表现得像D的单个
定义那样。
如果D的定义不满足这些
要求,则行为未定义。



Given the following:

template<typename T>
class A
{
public:
    static const unsigned int ID = ?;
};

I want ID to generate a unique compile time ID for every T. I've considered __COUNTER__ and the boost PP library but have been unsuccessful so far. How can I achieve this?

Edit: ID has to be usable as the case in a switch statement

Edit2: All the answers based on the address of a static method or member are incorrect. Although they do create a unique ID they are not resolved in compile time and therefore can not be used as the cases of a switch statement.

解决方案

This is sufficient assuming a standards conforming compiler (with respect to the one definition rule):

template<typename T>
class A
{
public:
    static char ID_storage;
    static const void * const ID;
};

template<typename T> char A<T>::ID_storage;
template<typename T> const void * const A<T>::ID= &A<T>::ID_storage;

From the C++ standard 3.2.5 One definition rule [basic.def.odr] (bold emphasis mine):

... If D is a template and is defined in more than one translation unit, then the last four requirements from the list above shall apply to names from the template’s enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

这篇关于编译时常数id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆