C ++标准说明int,long类型的大小是什么?

我正在寻找有关基本C ++类型大小的详细信息。 我知道这取决于体系结构(16位,32位,64位)和编译器。 ios

可是C ++有什么标准吗? git

我在32位体系结构上使用Visual Studio 2008。 这是我获得的: 测试

char  : 1 byte
short : 2 bytes
int   : 4 bytes
long  : 4 bytes
float : 4 bytes
double: 8 bytes

我试图找到没有可靠信息的可靠信息,这些信息说明了不一样体系结构和编译器下charshortintlongdoublefloat (以及其余我没有想到的类型)的大小。 ui


#1楼

咱们能够为类型定义同义词,以便咱们能够建立本身的“标准”。 this

在sizeof(int)== 4的机器上,咱们能够定义: lua

typedef int int32;

int32 i;
int32 j;
...

所以,当咱们将代码转移到long int的大小实际上为4的另外一台机器上时,咱们只需从新定义一次int便可。 spa

typedef long int int32;

int32 i;
int32 j;
...

#2楼

若是您对纯C ++解决方案感兴趣,那么我将使用模板和仅使用C ++标准代码在编译时根据位大小定义类型。 这使解决方案可跨编译器移植。 设计

背后的想法很简单:建立一个包含char,int,short,long,long long(有符号和无符号版本)类型的列表,而后扫描列表,并使用numeric_limits模板选择具备给定大小的类型。 code

包括此标头,您获得了8种类型stdtype :: int8,stdtype :: int16,stdtype :: int32,stdtype :: int64,stdtype :: uint8,stdtype :: uint16,stdtype :: uint32,stdtype :: uint64。 对象

若是没法表示某种类型,它将被评估为还在该标头中声明的stdtype :: null_type。

下文提供的代码没有任何担保,请仔细检查。
我也是METAPROGRAMMING的新手,随时能够编辑和更正此代码。
通过DevC ++测试(因此gcc版本约为3.5)

#include <limits>

namespace stdtype
{
    using namespace std;


    /*
     * THIS IS THE CLASS USED TO SEMANTICALLY SPECIFY A NULL TYPE.
     * YOU CAN USE WHATEVER YOU WANT AND EVEN DRIVE A COMPILE ERROR IF IT IS 
     * DECLARED/USED.
     *
     * PLEASE NOTE that C++ std define sizeof of an empty class to be 1.
     */
    class null_type{};

    /*
     *  Template for creating lists of types
     *
     *  T is type to hold
     *  S is the next type_list<T,S> type
     *
     *  Example:
     *   Creating a list with type int and char: 
     *      typedef type_list<int, type_list<char> > test;
     *      test::value         //int
     *      test::next::value   //char
     */
    template <typename T, typename S> struct type_list
    {
        typedef T value;
        typedef S next;         

    };




    /*
     * Declaration of template struct for selecting a type from the list
     */
    template <typename list, int b, int ctl> struct select_type;


    /*
     * Find a type with specified "b" bit in list "list"
     *
     * 
     */
    template <typename list, int b> struct find_type
    {   
        private:
            //Handy name for the type at the head of the list
            typedef typename list::value cur_type;

            //Number of bits of the type at the head
            //CHANGE THIS (compile time) exp TO USE ANOTHER TYPE LEN COMPUTING
            enum {cur_type_bits = numeric_limits<cur_type>::digits};

        public:
            //Select the type at the head if b == cur_type_bits else
            //select_type call find_type with list::next
            typedef  typename select_type<list, b, cur_type_bits>::type type;
    };

    /*
     * This is the specialization for empty list, return the null_type
     * OVVERRIDE this struct to ADD CUSTOM BEHAVIOR for the TYPE NOT FOUND case
     * (ie search for type with 17 bits on common archs)
     */
    template <int b> struct find_type<null_type, b>
    {   
        typedef null_type type;

    };


    /*
     * Primary template for selecting the type at the head of the list if
     * it matches the requested bits (b == ctl)
     *
     * If b == ctl the partial specified templated is evaluated so here we have
     * b != ctl. We call find_type on the next element of the list
     */
    template <typename list, int b, int ctl> struct select_type
    {   
            typedef  typename find_type<typename list::next, b>::type type; 
    };

    /*
     * This partial specified templated is used to select top type of a list
     * it is called by find_type with the list of value (consumed at each call)
     * the bits requested (b) and the current type (top type) length in bits
     *
     * We specialice the b == ctl case
     */
    template <typename list, int b> struct select_type<list, b, b>
    {
            typedef typename list::value type;
    };


    /*
     * These are the types list, to avoid possible ambiguity (some weird archs)
     * we kept signed and unsigned separated
     */

    #define UNSIGNED_TYPES type_list<unsigned char,         \
        type_list<unsigned short,                           \
        type_list<unsigned int,                             \
        type_list<unsigned long,                            \
        type_list<unsigned long long, null_type> > > > >

    #define SIGNED_TYPES type_list<signed char,         \
        type_list<signed short,                         \
        type_list<signed int,                           \
        type_list<signed long,                          \
        type_list<signed long long, null_type> > > > >



    /*
     * These are acutally typedef used in programs.
     * 
     * Nomenclature is [u]intN where u if present means unsigned, N is the 
     * number of bits in the integer
     *
     * find_type is used simply by giving first a type_list then the number of 
     * bits to search for.
     *
     * NB. Each type in the type list must had specified the template 
     * numeric_limits as it is used to compute the type len in (binary) digit.
     */
    typedef find_type<UNSIGNED_TYPES, 8>::type  uint8;
    typedef find_type<UNSIGNED_TYPES, 16>::type uint16;
    typedef find_type<UNSIGNED_TYPES, 32>::type uint32;
    typedef find_type<UNSIGNED_TYPES, 64>::type uint64;

    typedef find_type<SIGNED_TYPES, 7>::type    int8;
    typedef find_type<SIGNED_TYPES, 15>::type   int16;
    typedef find_type<SIGNED_TYPES, 31>::type   int32;
    typedef find_type<SIGNED_TYPES, 63>::type   int64;

}

#3楼

正如其余人回答的那样,“标准”都将大多数细节保留为“实现定义”,仅声明“ char”类型的宽度为“ char_bis”,而“ char <= short <= int <= long < = long long”(float和double与IEEE浮点标准很是一致,long double一般与double相同-但在更多当前的实现中可能更大)。

之因此没有很是具体和确切的值,部分缘由是由于诸如C / C ++之类的语言被设计为可移植到大量硬件平台上,包括“ char”字长可能为4位的计算机系统。或7位,甚至是普通家用计算机用户所使用的“ 8- / 16- / 32- / 64位”计算机之外的其余值。 (此处的字长表示系统正常运行多少位宽-一样,它并不老是像家用计算机用户所指望的那样是8位宽。)

若是您确实须要一个特定位数的对象(从一系列表明整数值的位的意义上来讲),则大多数编译器都有某种方法来指定它; 但这一般是不可移植的,即便是在ame公司制造的编译器之间,也适用于不一样的平台。 一些标准和实践(尤为是limits.h等)足够广泛,以至大多数编译器将支持肯定特定值范围的最佳匹配类型,而不是所使用的位数。 (也就是说,若是您知道须要保持0到127之间的值,则能够肯定编译器支持“ int8”类型的8位,这足以容纳所需的整个范围,但不能知足“ int7”类型,它将与7位彻底匹配。)

注意:许多Un * x源程序包使用“ ./configure”脚本,该脚本将探查编译器/系统的功能并输出合适的Makefile和config.h。 您可能会检查其中一些脚本,以了解它们如何工做以及如何检测编译器/系统功能,并遵循它们的指导。


#4楼

当涉及不一样体系结构和不一样编译器的内置类型时,只需在您的体系结构上与编译器一块儿运行如下代码,以查看其输出。 下面显示了个人Ubuntu 13.04 (Raring Ringtail)64位g ++ 4.7.3输出。 还请注意下面的回答,这就是为何按如下顺序订购输出的缘由:

“有五种标准的带符号整数类型:带符号的char,short int,int,long int和long long int。在此列表中,每种类型提供的存储量至少与列表中位于其前面的类型相同。”

#include <iostream>

int main ( int argc, char * argv[] )
{
  std::cout<< "size of char: " << sizeof (char) << std::endl;
  std::cout<< "size of short: " << sizeof (short) << std::endl;
  std::cout<< "size of int: " << sizeof (int) << std::endl;
  std::cout<< "size of long: " << sizeof (long) << std::endl;
  std::cout<< "size of long long: " << sizeof (long long) << std::endl;

  std::cout<< "size of float: " << sizeof (float) << std::endl;
  std::cout<< "size of double: " << sizeof (double) << std::endl;

  std::cout<< "size of pointer: " << sizeof (int *) << std::endl;
}


size of char: 1
size of short: 2
size of int: 4
size of long: 8
size of long long: 8
size of float: 4
size of double: 8
size of pointer: 8

#5楼

unsigned char bits = sizeof(X) << 3;

其中Xcharintlong等。将为您提供X大小(以位为单位)。

相关文章
相关标签/搜索