Boost logo

Boost :

Subject: Re: [boost] Detection of 64bit using macro
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2010-08-14 06:32:13


On 08/13/2010 11:44 PM, Kim Kuen Tang wrote:
>
> Hi all,
>
> the question might be not directly related to boost. But i just want to
> be sure that i am not reventing the wheel.
> I am searching for a macro that is able to detect that a system is 32bit
> or 64bit no matter what compiler is used.
> Is there something similar already implemented in boost?

There is no standard macro to detect this, so every solution will be
platform-specific. For instance, I think, GCC defines some macro that
has value of sizeof(void*). On platforms with uintptr_t you can test for
UINTPTR_MAX macro value. On linux there is __WORDSIZE macro that has
value of number of bits in the native integer type (that is, 32 or 64).
Some compilers may define their own specific macros:

<http://predef.sourceforge.net/prearch.html>

> Perhaps some background about this feature.
>
> Template specialization for std::size_t is not needed, when the
> specialization already exists for unsigned int. But on a 64bit system
> this is not true.
> So you need explicitly added the specialization whenever a 64bit
> compiler is used.
>
> See the following example.
>
> # include <cstddef>
>
> template<typename T>
> struct Null;
>
> template<> struct Null<unsigned int> {};
> template<> struct Null<std::size_t> {}; // compile only on a 64bit compiler
>
> int main()
> {
> return 0;
> };
>
> Thx for any comments, help or suggestions

In this case I would do something like this:

   template< typename T >
   struct Null:
     public NullImpl< T, sizeof(T) >
   {
   };

and then specialize NullImpl on the size of the type, rather than the
type itself.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk