One of the boost cross-platform C ++ integer types, fixed-width integer

Source: Internet
Author: User

The cross-platform Integer type of Ace has always been used, such as ace_uint32. However, the C ++ style is obviously modern, and the model is dominant, so recently, we began to gradually replace ace the ace code and use the boost library instead.

In the boost library, standard integer types is used to support integer types across platforms. I use version 1.48. For more information, see:

Http://www.boost.org/doc/libs/1_48_0/libs/integer/doc/html/boost_integer/cstdint.html

The integer Implementation of the boost library is based on the 99 C standard. The 98C ++ standard is not selected because of undefined behaviors in some cases. In the future, if the new C ++ Standard specifies the cross-platform Integer type, the boost library may be abolished. However, C ++ 11 has not yet been fully supported by the compiler, and there is not much choice.

You can implement it by yourself, but it means that distribution is difficult and many tests are available. You can understand how boost is implemented. You don't have to do it yourself.

Note that the type and template starting with boost: are always used. Do not directly use the standard type and macro of C introduced by boost.

Start now.

Boost provides the integer type of the exact width, which is named after int # _ T, and # is the number of digits. For example, int8_t represents a signed 8-digit integer. So what is its true counterpart?

Note: Here I am Ubuntu 64bit, gcc4.6.3, boost 1.48.0

/* For GCC 2.7 and later, we can use specific type-size attributes.  */# define __intN_t(N, MODE) \  typedef int int##N##_t __attribute__ ((__mode__ (MODE)))# define __u_intN_t(N, MODE) \  typedef unsigned int u_int##N##_t __attribute__ ((__mode__ (MODE)))# ifndef __int8_t_defined#  define __int8_t_defined__intN_t (8, __QI__);__intN_t (16, __HI__);__intN_t (32, __SI__);__intN_t (64, __DI__);# endif

After macro replacement, it is actually:

typedef int int8_t __attribute__ ((__mode__ (__QI__)))

Commands using several GCC Compilers

_ Attribute __, _ mode and _ Qi __

# Is the macro Connection Symbol.

This post explains these commands: http://stackoverflow.com/questions/4559025/what-does-gcc-attribute-modexx-actually-do

_ Qi _ represents the minimum addressing unit, one byte, eight bits.

_ Hi _ and the subsequent commands are multiples of 8 bits.

For the unsigned fixed-width Integer type, the U is added before it, for example, uint # _ T is the fixed-width expression of the unsigned integer.

Another set of typedef is used, and the preceding compiler commands are not used.

/* Unsigned.  */typedef unsigned charuint8_t;typedef unsigned short intuint16_t;#ifndef __uint32_t_definedtypedef unsigned intuint32_t;# define __uint32_t_defined#endif#if __WORDSIZE == 64typedef unsigned long intuint64_t;#else__extension__typedef unsigned long long intuint64_t;#endif

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.