C++ 检查C+中的类型+;11#定义
我想根据C++ 检查C+中的类型+;11#定义,c++,c++11,typedef,C++,C++11,Typedef,我想根据typedef的值进行预处理器定义 其思想是检查索引类型\u t,并生成相应的索引类型\u GL定义 以下操作不符合预期 typedef uint32_t index_type_t; #ifdef INDEX_TYPE_GL #undef INDEX_TYPE_GL #endif #if (index_type_t == uint8_t) #define INDEX_TYPE_GL GL_UNSIGNED_BYTE #elif (index_type_t == uint32_t)
typedef
的值进行预处理器定义
其思想是检查索引类型\u t
,并生成相应的索引类型\u GL
定义
以下操作不符合预期
typedef uint32_t index_type_t;
#ifdef INDEX_TYPE_GL
#undef INDEX_TYPE_GL
#endif
#if (index_type_t == uint8_t)
#define INDEX_TYPE_GL GL_UNSIGNED_BYTE
#elif (index_type_t == uint32_t)
#define INDEX_TYPE_GL GL_UNSIGNED_INT
#elif (index_type_t == uint16_t)
#deine INDEX_TYPE_GL GL_UNSIGNED_SHORT
#endif
请注意,uint8\u t
、uint16\u t
和uint32\u t
在其他地方是独立的typedef
,而GL\u无符号字节
等是整数值,而不是类型。枚举样式:
template<typename T> struct IndexTypeGL {};
template<> struct IndexTypeGL<uint8_t> { enum {value = GL_UNSIGNED_BYTE }; };
template<> struct IndexTypeGL<uint16_t> { enum {value = GL_UNSIGNED_SHORT}; };
template<> struct IndexTypeGL<uint32_t> { enum {value = GL_UNSIGNED_INT }; };
#define INDEX_TYPE_GL IndexTypeGL<index_type_t>::value
模板结构IndexTypeGL{};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_BYTE};};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_SHORT};};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_INT};};
#定义索引类型索引类型:值
“静态constexpr”样式
模板结构IndexTypeGL{};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_BYTE;};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_SHORT;};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_int;};
枚举样式:
template<typename T> struct IndexTypeGL {};
template<> struct IndexTypeGL<uint8_t> { enum {value = GL_UNSIGNED_BYTE }; };
template<> struct IndexTypeGL<uint16_t> { enum {value = GL_UNSIGNED_SHORT}; };
template<> struct IndexTypeGL<uint32_t> { enum {value = GL_UNSIGNED_INT }; };
#define INDEX_TYPE_GL IndexTypeGL<index_type_t>::value
模板结构IndexTypeGL{};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_BYTE};};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_SHORT};};
模板结构IndexTypeGL{enum{value=GL_UNSIGNED_INT};};
#定义索引类型索引类型:值
“静态constexpr”样式
模板结构IndexTypeGL{};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_BYTE;};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_SHORT;};
模板结构IndexTypeGL{static constexpr int value=GL_UNSIGNED_int;};
<代码> >值>代码> GLUN UNSIGNEDYBYECT/COMPUT>等是整数。您不能使用这种方法,因为预处理器不知道Type Debug,或者任何其他C++构造。你可以做的是有一个Type或const值依赖于实际的索引类型。我可能会怎么回答?JAROD42只是回答:-不完全是,值<代码> GLU-unsiNedDyByth[/Cuff] >是整数。你不能用这个方法,因为预处理器不知道Type,或者任何其他C++结构。你能做的就是让一个typedef或const值取决于索引的实际类型。我该怎么做呢?Jarod42刚刚回答说:-)天才之笔,谢谢Dprefer静态常量
到枚举hack@Manu343726:static constexpr优于static const。enum和const之间的胜利者似乎不清楚。@Manu343726您能给出一个使用static const
的解决方案示例吗?@cmbasnetttemplate struct IndexTypeGL{static constexpr value=GL_UNSIGNED_BYTE;}代码>天才之笔,谢谢!:Dprefer静态常量
到枚举hack@Manu343726:static constexpr优于static const。enum和const之间的胜利者似乎不清楚。@Manu343726您能给出一个使用static const
的解决方案示例吗?@cmbasnetttemplate struct IndexTypeGL{static constexpr value=GL_UNSIGNED_BYTE;}代码>