Use the std::conditional
meta-function from C++11.
#include <type_traits> //include this
typedef std::conditional<sizeof(int) <= 4,
std::mt19937,
std::mt19937_64>::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<sizeof(T) <= 4, // T is template parameter
std::mt19937,
std::mt19937_64>::type Engine;
Or make Engine
depend on T
as:
template<typename T>
using Engine = typename std::conditional<sizeof(T) <= 4,
std::mt19937,
std::mt19937_64>::type;
That is flexible, because now you can use it as:
Engine<int> engine1;
Engine<long> engine2;
Engine<T> engine3; // where T could be template parameter!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…