Boost logo

Boost :

Subject: Re: [boost] Detection of 64bit using macro
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-08-16 07:02:26

Jeffrey Lee Hellrung, Jr.
> On 8/13/2010 12:44 PM, Kim Kuen Tang wrote:
> >
> > 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?

We define an in-house manifest constant based upon the platform-specific manifest constants.

> > 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.

That depends upon the platform as integer widths vary among platforms: ILP64, LP64, LLP64, ILP32, etc.

> > So you need explicitly added the specialization whenever a 64bit
> > compiler is used.

The specializations should be based upon the supported sizes.

> > # 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;
> > };
> Perhaps you should just specialize on std::size_t conditional on it
> being different from an unsigned int, rather than on 64bit-ness:
> template< class T, bool = boost::is_same< T, unsigned int >::value >
> struct Null2;
> template<>
> struct Null2< unsigned int, true > { ... };
> template<>
> struct Null2< std::size_t, false > { ... };
> template< class T >
> struct Null : Null2<T> { };

The problem with that approach is that size_t may be the same size as another type on another platform. The better approach is to use fixed size integer types to get specializations for integer types of various numbers of bits: 8, 16, 32, 64, etc.. Platform-specific manifest constants will be necessary to determine whether a platform supports 64b or larger integer types and to know the type names. (Whether you abstract those differences, beyond what Boost provides, depends upon your specific needs.) That done, you'll have a specialization for size_t and any other supported integer type.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at