Detection of 64bit using macro

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? 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 Kim

On 8/13/2010 12: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?
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
Kim
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> { }; There might be a "cleaner" way to achieve the same effect, but does that help? - Jeff

Hi Jeff, Jeffrey Lee Hellrung, Jr. schrieb:
Perhaps you should just specialize on std::size_t conditional on it being different from an unsigned int, rather than on 64bit-ness:
nice solution to my problem. Thx. But i am still interested in the answer to my question. -Kim
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> { };
There might be a "cleaner" way to achieve the same effect, but does that help?
- Jeff _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

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@sig.com Software Engineer, Core Software using std::disclaimer; Susquehanna International Group, LLP http://www.sig.com 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.

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.

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).
You also need to specifiy what you mean by "64 bit", do you mean 64-bit pointers, or 64-bit integers or both, or something else, as these can all vary independently of each other. John.

On 08/14/2010 04:05 PM, John Maddock wrote:
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).
You also need to specifiy what you mean by "64 bit", do you mean 64-bit pointers, or 64-bit integers or both, or something else, as these can all vary independently of each other.
It's not that I disagree with you, but usually platforms are referred to as 64-bit when a program is able to address 64 bit address range, which means that the pointer type has to be at least 64-bit. The support for 64-bit integers is much less connected to the platform architecture.
participants (5)
-
Andrey Semashev
-
Jeffrey Lee Hellrung, Jr.
-
John Maddock
-
Kim Kuen Tang
-
Stewart, Robert