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大小(以位爲單位)。

相關文章
相關標籤/搜索