Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a conditional typedef in C++

Tags:

c++

c++11

I am trying to do something like this:

#include <iostream> #include <random>  typedef int Integer;  #if sizeof(Integer) <= 4     typedef std::mt19937     Engine; #else     typedef std::mt19937_64  Engine; #endif  int main() {     std::cout << sizeof(Integer) << std::endl;     return 0; } 

but I get this error:

error: missing binary operator before token "(" 

How can I correctly make the conditional typedef?

like image 551
Martin Drozdik Avatar asked Jul 25 '13 09:07

Martin Drozdik


2 Answers

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! 
like image 142
Nawaz Avatar answered Sep 19 '22 02:09

Nawaz


Using std::conditional you can do it like so:

using Engine = std::conditional<sizeof(int) <= 4,                                 std::mt19937,                                 std::mt19937_64                                >::type; 

If you want to do a typedef, you can do that too.

typedef std::conditional<sizeof(int) <= 4,                           std::mt19937,                           std::mt19937_64                          >::type Engine 
like image 40
Rapptz Avatar answered Sep 19 '22 02:09

Rapptz