93

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?

7
  • 25
    The preprocessor doesn't know anything about sizeof, or other C++ constructs. It certainly doesn't know about things you created yourself with typedef, as that hasn't even been parsed yet. Jul 25, 2013 at 9:44
  • 2
    You could use enable_if or conditional to conditionally define typedefs, but you can't use preprocessor for that. Jul 25, 2013 at 9:44
  • 1
    @LightnessRacesinOrbit: Preprocessing and compilation are integrated in GCC, so it is not only not certain that the software processing code does not know about user-created type definitions but known to be false in the case of GCC. The reason sizeof cannot work in a preprocessor conditions is because the language is defined that way, not because of how an implementation works. Aug 28, 2013 at 15:16
  • 1
    @LightnessRacesinOrbit: The translation phases define syntax and semantics, not order of processing. Per C++ 2011 (N3092) 2.2 [lex.phases] note 11, “Implementations must behave as if these separate phases occur, although in practice different phases might be folded together.” My point about GCC is relevant because it demonstrates that your claim that this is how an implementation works is wrong. In other words, your comment claims that a particular method of implementation prevents this. But it is not implementation that prevents this (we could do it); it is the language definition. Aug 28, 2013 at 15:35
  • 1
    @Eric: I didn't mean to claim anything about implementations whatsoever. I certainly didn't mention any particular one. My comment stated a behaviour that is subject to the as-if rule, just as do yours. I don't think we're actually disagreeing on anything here — your language lawyering might as well have come straight from the mirror. :) Aug 28, 2013 at 17:09

3 Answers 3

144

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!
5
  • 4
    +1 Minor nitpick: Checking for sizeof(int) <= 4 is perhaps not a very portable way since on a 64-bit Windows machine, GCC (MinGW) x64 compiler gives sizeof(int) = sizeof(long) = 4. A better way would be sizeof(void*) <= 4.
    – legends2k
    Jul 25, 2013 at 13:12
  • @legends2k: You mean Engine<void*> engine4;? ;-)
    – Nawaz
    Jul 25, 2013 at 13:32
  • 2
    @Nawaz: Of course not :) I meant std::conditional<sizeof(void*) <= 4, std::mt19937, std::mt19937_64> in the first code snippet.
    – legends2k
    Jul 25, 2013 at 13:50
  • 1
    @legends2k: Why would you use that if I've provided you with Engine<void*>? :P
    – Nawaz
    Jul 25, 2013 at 13:59
  • @Nawaz: Haha... that's true. However, I thought the OP should probably know the pitfall in detecting the architecture based on the size of an int :)
    – legends2k
    Jul 25, 2013 at 15:32
36

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
3
  • There's no need for typename here
    – gx_
    Jul 25, 2013 at 9:46
  • @gx_ Yeah, used to putting it there from working with templates, not concrete types.
    – Rapptz
    Jul 25, 2013 at 9:47
  • 1
    @LightnessRacesinOrbit Well, I fixed it a little.
    – Rapptz
    Jul 25, 2013 at 10:23
5

If you don't have C++11 available (although it appears you do if you're planning to use std::mt19937), then you can implement the same thing without C++11 support using the Boost Metaprogramming Library (MPL). Here is a compilable example:

#include <boost/mpl/if.hpp>
#include <iostream>
#include <typeinfo>

namespace mpl = boost::mpl;

struct foo { };
struct bar { };

int main()
{
    typedef mpl::if_c<sizeof(int) <= 4, foo, bar>::type Engine;

    Engine a;
    std::cout << typeid(a).name() << std::endl;
}

This prints the mangled name of foo on my system, as an int is 4 bytes here.

2
  • 1
    Why don't you use if_c instead? It would be must easier to write (and understand) : mpl::if_c<sizeof(int)<=4, foo, bar>::type. Isn't it?
    – Nawaz
    Jul 25, 2013 at 17:12
  • 1
    @Nawaz: Indeed, that is better in a number of ways. I had forgotten about mpl::if_c. I updated the example to use that approach instead.
    – Jason R
    Jul 25, 2013 at 17:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.