c++ #elif指令被丢弃

C++ #elif directive is being discarded

本文关键字:指令 #elif c++      更新时间:2023-10-16

我正在尝试创建一个基于边界的类型声明

template<class B>
struct IntDecl {
enum {
    L = B::_l, U = B::_u
};
 #if (L >=0 && U <=255)
  typedef char Type;
 #elif (L>=0&&U<=65535) 
  typedef unsigned int Type;
 #endif
};

因此,正如您在这里看到的,根据LU的值来定义类型。例如

IntDecl< BOUND < 0, 65535 > >::Type i; // this should be a unsigned int 
IntDecl< BOUND < 0, 255 > >::Type i1;  // this should be a char

问题是,(ii1)都被认为是chars,换句话说,#elif被丢弃了。任何帮助吗?为什么#elif没有执行?

预处理过程在语义分析之前进行,enum是一个语义结构。您需要使用模板来实现这一点,或者创建定义预处理器常量的LU宏。