-
May 1st, 2008, 02:26 PM
#1
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:
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!
-
May 1st, 2008, 03:32 PM
#2
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.
-
May 1st, 2008, 03:37 PM
#3
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!
-
May 1st, 2008, 04:55 PM
#4
Re: Comma usage in initializers
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.
-
May 1st, 2008, 05:03 PM
#5
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:
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!
-
May 1st, 2008, 11:46 PM
#6
Re: Comma usage in initializers
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|