What are primitive types default-initialized to in C++?

前端 未结 6 1939
无人及你
无人及你 2020-11-27 14:05

When I use an initialization list:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

the primitive type (

6条回答
  •  清酒与你
    2020-11-27 14:32

    You are not correct. The object is not default-initialized but value-initialized. And its value is well-defined

    int = 0, 
    bool = false, 
    float = 0.0f,  
    enum = (enum type)0, 
    pointer = null pointer
    pointer to member = null member pointer
    

    Note that zero is in the range of values for any enumeration, even if it doesn't contain an explicit enumerator with that vaue, so it's safe to initialize an enumeration variable to that value.

    In particular for pointer to data members, the representation used in practice is not all-zero bits. In the so-called C++ Itanium ABI used by at least GCC and Clang, pointer to data members have an all-one bits null representation.

提交回复
热议问题