CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Comma usage in initializers

    Consider the following example:

    Code:
    int foo[] = { 3, 4, 5, };
    Note that above I have a comma operator after the last element in the initializer. An example presenting the exact same behavior can be found in the standard at 8.5.1.11:

    Quote Originally Posted by C++03 Standard : Paragraph 8.5.1.11
    Code:
    float y[4][3] = {
        { 1, 3, 5 },
        { 2, 4, 6 },
        { 3, 5, 7 },
    };
    However, I can't find anywhere in the standard that describes that this behavior is allowed. The standard has always been difficult for me to read, since it doesn't always find the most intuitive way of explaining things. Could someone reference me to the paragraph or set of paragraphs that explains the behavior of placing a comma after the last item in an initializer?
    --MrDoomMaster
    --C++ Game Programmer


    Don't forget to rate me if I was helpful!

  2. #2
    Join Date
    Oct 2004
    Posts
    296

    Re: Comma usage in initializers

    Could someone reference me to the paragraph or set of paragraphs that explains the behavior of placing a comma after the last item in an initializer?
    No. But the comma has no effect. A similar thing can be seen here:
    Code:
    	int nums[3] = {};
    	int nums2[3] = {0};
    where the 0 has no effect, although it is clearer.

    C++ in a Nutshell seems to think that being able to use a trailing comma is only for your cut and pasting convenience. For instance, if you had to write:
    Code:
    string words[10] = {
    		"hello",
    		"goodbye", 
    		"stop",
    		"go"
    	};
    then if you cut and pasted the last line to the beginning of the array, you would have to delete the comma after "stop".
    Last edited by 7stud; May 1st, 2008 at 03:35 PM.

  3. #3
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Comma usage in initializers

    Everything you've just said I already understood, however your first code example is confusing, I'm not sure what you're trying to point out.

    I'm specifically expecting this behavior to be outlined in the standard, otherwise how do all of the compiler manufacturers out there know to maintain this specific behavior?
    --MrDoomMaster
    --C++ Game Programmer


    Don't forget to rate me if I was helpful!

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Comma usage in initializers

    Quote Originally Posted by MrDoomMaster
    Could someone reference me to the paragraph or set of paragraphs that explains the behavior of placing a comma after the last item in an initializer?
    I can't find one. I just spent some time looking myself and couldn't find any. Chances are that the std doesn't address the issue and that the compiler writers are doing you a favor, as 7STUD implies. If I were you, I would avoid the trailing comma. I have seen situations where some tool that autogenerates code, in a loop, outputs an initializer with a trailing comma and there isn't much you can do about it. Every compiler I have used typically outputs a warning.

    Have you tried searching the stds document for the phrase "no effect"? If you are really concerned about it that is what I would do. Usually the phrase "has no effect" is specifically stated for this kind of thing. But I cannot find anything that addresses your specific concern. Sorry.

    Write a code snip that causes the warning and then research the warning number in your compiler docs.See if it can be traced back to something in a std. I doubt it but it's worth a shot.

  5. #5
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Comma usage in initializers

    The reason why I am bringing this up is because Microsoft asserts that the C++ language allows the behavior. I quote from one of the blogs posted to the Visual Studio Developer Blog:

    Quote Originally Posted by Microsoft
    C++ allows for a comma after the last entry of the enum and the array initializer.
    The full blog can be found here.
    --MrDoomMaster
    --C++ Game Programmer


    Don't forget to rate me if I was helpful!

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Comma usage in initializers

    Quote Originally Posted by MrDoomMaster
    However, I can't find anywhere in the standard that describes that this behavior is allowed. The standard has always been difficult for me to read, since it doesn't always find the most intuitive way of explaining things. Could someone reference me to the paragraph or set of paragraphs that explains the behavior of placing a comma after the last item in an initializer?
    Take a look at 8.5 [dcl.init]/1:
    Code:
    initializer-clause:
            assignment-expression
            { initializer-list ,opt }
            { }
    Doesn't have to be a full initializer list. You can have an array of 10 elements and the initializer specifies just 5 and can end with a ','. opt, most probably is being referred to as optional.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured