Explicit initialization of class members

This is a discussion on Explicit initialization of class members within the C++ Programming forums, part of the General Programming Boards category; Code: struct some_struct{ int a; }; some_struct n = {}; n.a will be 0; I know this braces form of ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Explicit initialization of class members

    Code:
    struct some_struct{
        int a;
    };
    some_struct n = {};
    n.a will be 0;

    I know this braces form of initialization is inherited from C and is supported for compatibility with C programs, but this only compiles with C++, not with the C compiler. I'm using Visual C++ 2005.

    In C this type of initialization
    Code:
    some_struct n = {0};
    is correct and will zero-initialize all members of a structure.

    Is the empty pair of braces form of initialization standard? Because I've never seen it before, but seem to be used in winapi.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, I'm pretty sure it is standard. The reason it doesn't compile in C is probably because structs have their separate namespace there. So you need to write
    Code:
    struct some_struct n = {};
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    No, it won't compile in C, at least not in Visual C++ 2005.

    In C you can write
    Code:
    typedef struct {
        int a;
    }some_struct;
    to be able to use
    Code:
    some_struct n;
    but if I write
    Code:
    some_struct n = {0};
    it compiles.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by zephon View Post
    No, it won't compile in C, at least not in Visual C++ 2005.
    That is a very contradictory observation if you think about it

    In any event, the construct is valid in both C and C++. Although I tend to see it discouraged in practice (disallowed in some style guides, compilers often warn about such a construct at higher warning settings, etc).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by grumpy View Post
    That is a very contradictory observation if you think about it
    Yes that's because the same IDE (Visual C++ 2005 known also as Visual C++ 8.0) alows to compile your code as C or as C++. Also the compiler is invoked indirectly using a command-line interface that is CL.EXE, and I don't know the compiler names that this tool calls.

    Quote Originally Posted by grumpy View Post
    In any event, the construct is valid in both C and C++. Although I tend to see it discouraged in practice (disallowed in some style guides, compilers often warn about such a construct at higher warning settings, etc).
    This is not valid in C.
    The standard (C99 - 6.7.8/12 - Initialization) says this:
    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    In C the grammar requires that there be at least one 'assignment-expression' inside the braces. An 'assignment-expression' can be many things from a constant or identifier up through much more complex expressions. However, an empty string doesn't qualify as an 'assignment-expression', so there has to be something between the braces.

    In C++, the grammar specifically allows the empty braces initializer.
    Source

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Unfortunately, your source is incorrect.

    Firstly (minor point - they made a typo) the words it attributes as C99 - 6.7.8/12 is actually Section 6.7.8 para 21.

    Second, there is no statement in the C standard that the gramma requires at least one expression inside the braces.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, this is what the C99 final public draft says (don't have an official version):
    Code:
    6.7.8 Initialization Syntax
    
    initializer:
        assignment-expression
        { initializer-list }
        { initializer-list , }
    
    initializer-list:
        designation[opt] initializer
        initializer-list , designation[opt] initializer
    So initializer-list consists of at least a single initializer, and initializer in its list form needs an initializer-list between the braces. So yeah, the grammar requires at least one initializer.

    To contrast, here's the equivalent section of the latest C++0x draft (I do have a C++03 official standard somewhere, but can't find it right now):
    Code:
    8.5 Initializers [dcl.init]
    [...]
    initializer:
        brace-or-equal-initializer
        ( expression-list )
    
    brace-or-equal-initializer:
        = initializer-clause
        braced-init-list
    
    initializer-clause:
        assignment-expression
        braced-init-list
    
    initializer-list:
        initializer-clause ...opt
        initializer-list , initializer-clause ...opt
    
    braced-init-list:
        { initializer-list ,opt }
        { }
    The empty braces are an explicit addition to the grammar.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-12-2009, 04:46 AM
  2. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  5. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 03:52 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21