且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编译时常数id

更新时间:2022-10-14 21:37:42

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

  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.