简体   繁体   English

如何在编译时检查类型

[英]How to check type on compile time

I'm giving option to compile the program with either float or double type, but there is a problem: I need to manually set either GL_FLOAT or GL_DOUBLE, because I dont know how can I do the following: 我可以选择使用float或double类型编译程序,但是有一个问题:我需要手动设置GL_FLOAT或GL_DOUBLE,因为我不知道如何执行以下操作:

typedef float MYTYPE;

#if MYTYPE == float
    #define GL_MYTYPE GL_FLOAT // used for vertex array parameters.
#else
    #define GL_MYTYPE GL_DOUBLE
#endif

Note: I dont have C++11 or whatsoever, just the good old C++. 注意:我没有C ++ 11或者任何东西,只有好的旧C ++。

In C++11, you can use std::conditional and std::is_same as: 在C ++ 11中,您可以使用std::conditionalstd::is_same作为:

#define GL_MYTYPE  std::conditional                       \
                   <  std::is_same<MYTYPE,float>::value,  \
                      GL_FLOAT,                           \
                      GL_DOUBLE                           \
                   >::type                            

In C++03, you can implement these functionalities yourself as: 在C ++ 03中,您可以自己实现这些功能:

template<bool B, class T, class F>
struct conditional { typedef T type; };

template<class T, class F>
struct conditional<false, T, F> { typedef F type; };

and

template<class T, class U>
struct is_same { static const bool value = false; };

template<class T>
struct is_same<T, T> { static const bool value = true; };

Note that the implementation of conditional is taken from the site itself. 请注意, conditional的实现取自站点本身。

#define s are processed by the preprocessor, before the compiler runs. 在编译器运行之前,预处理器会处理#define So it doesn't see the typedef . 所以它没有看到typedef

Use preprocessor macros to change what your typedef line does 使用预处理器宏来更改typedef行的功能

#if defined(TYPE_IS_FLOAT)
typedef float MYTYPE;
#else
typedef double MYTYPE;
#end

And then use your build system to set TYPE_IS_FLOAT as appropriate (and give it a better name of course). 然后使用您的构建系统来适当地设置TYPE_IS_FLOAT (并为其提供更好的名称)。

Or wrap all the code which behaves like this up in a template class and use a template parameter for this type, then use whichever version is appropriate at the time. 或者将所有类似行为的代码包装在模板类中,并使用此类型的模板参数,然后使用当时适合的版本。

You can do it this way: 你可以这样做:

#define MYFLOAT

#ifdef MYFLOAT
    typedef float MYTYPE;
     #define GL_MYTYPE GL_FLOAT // used for vertex array parameters.
#else
    typedef double MYTYPE;
    #define GL_MYTYPE GL_DOUBLE
#endif

You would need to define MYFLOAT if you want to use float , or omit it to use double . 如果要使用float ,则需要定义MYFLOAT ,或者省略它以使用double

PS: keep in ind this is not evaluated at compile time, but at pre-processing time. PS:保持在ind中,这不是在编译时评估,而是在预处理时。

You may use #ifdef as follow : 你可以使用#ifdef如下:

#ifdef DOUBLE
   #define GL_MYTYPE GL_FLOAT
   //or
   typdef ...
#endif

#ifdef FLOAT
   #define GL_MYTYPE GL_DOUBLE
   //or
   typdef ...
#endif

You have to compile your code with DOUBLE or FLOAT as the symblos defined. 您必须使用DOUBLE或FLOAT编译代码作为定义的符号。

I think Nawaz's answer is nearly correct. 我认为Nawaz的答案 几乎是正确的。 In the special case of choosing between GL_FLOAT and GL_DOUBLE , std::conditional is not needed. GL_FLOATGL_DOUBLE之间进行选择的特殊情况下, GL_DOUBLE std::conditional Just use a ternary if : 如果出现以下情况,请使用三元

#define GL_MYTYPE std::is_same<MYTYPE,float>::value?GL_FLOAT:GL_DOUBLE

This is because GL_FLOAT and GL_DOUBLE are not types, but rather literal GLenum values. 这是因为GL_FLOATGL_DOUBLE不是类型,而是文字GLenum值。 So GL_MYTYPE should also not be a typename but a GLenum . 因此GL_MYTYPE也不应该是类型名称而是GLenum So I'd rather use: 所以我宁愿使用:

const GLenum GL_MYTYPE = std::is_same<MYTYPE,float>::value?GL_FLOAT:GL_DOUBLE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM