How to make a conditional typedef in C++

后端 未结 3 912
野性不改
野性不改 2020-12-12 10:43

I am trying to do something like this:

#include 
#include 

typedef int Integer;

#if sizeof(Integer) <= 4
    typedef std::         


        
3条回答
  •  再見小時候
    2020-12-12 10:55

    Use the std::conditional meta-function from C++11.

    #include   //include this
    
    typedef std::conditional::type Engine;
    

    Note that if the type which you use in sizeof is a template parameter, say T, then you have to use typename as:

    typedef typename std::conditional::type Engine;
    

    Or make Engine depend on T as:

    template
    using Engine = typename std::conditional::type;
    

    That is flexible, because now you can use it as:

    Engine  engine1;
    Engine engine2;
    Engine    engine3; // where T could be template parameter!
    

提交回复
热议问题