Thread: comma at end of intialization list

  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    comma at end of intialization list

    If we have a piece of code like this:
    Code:
    int arr[] = 
    {
       1, 
       2, 
    #if SOMETHING
       3,
    #endif
    #if SOMEOTHER
       4
    #endif
    };
    there is a dangling comma after 2 if SOMETHING and SOMEOTHER is false, or after 3 if SOMETHING is true, but SOMEOTHER is false.

    Is this valid in the current standard of C++?

    [The compilers I've used accepts it - so it's more a question of "are we allowed to do this".]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it is permitted by the C++ Standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think your example case, and the case of auto-generated code are the reason why this sort of code is allowed. Sadly, 99% of my code almost always has it there just in case I need to add or subtract from an array on the fly.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, in my particular case I was trying to fix a bug where the code wouldn't compile with this:
    Code:
       1, 
       2, 
    #if SOMETHING
       3
    #endif
    #if SOMEOTHER
       ,
       4
    #endif
    when "SOMETHING" wasn't true, and I don't really want to break the code by introducing something that won't compile when using a "strict standard" compiler - and I didn't want to introduce an #else branch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I always use an "extra" comma at the end of such lists, but in case you absolutely want a minimal amount of commas you could try something like this:
    Code:
    int arr[] = 
    {
       1 
       ,2 
    #if SOMETHING
       ,3
    #endif
    #if SOMEOTHER
       ,4
    #endif
    };
    A bit odd, but hey - the choice is yours
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8
    Quote Originally Posted by Magos View Post
    Code:
    int arr[] = 
    {
       1 
       ,2 
    #if SOMETHING
       ,3
    #endif
    #if SOMEOTHER
       ,4
    #endif
    };
    For reasons of which I am no longer aware, I have always done it this way.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by matsp View Post
    If we have a piece of code like this:
    Code:
    int arr[] = 
    {
       1, 
       2, 
    #if SOMETHING
       3,
    #endif
    #if SOMEOTHER
       4
    #endif
    };
    there is a dangling comma after 2 if SOMETHING and SOMEOTHER is false, or after 3 if SOMETHING is true, but SOMEOTHER is false.

    Is this valid in the current standard of C++?

    [The compilers I've used accepts it - so it's more a question of "are we allowed to do this".]

    --
    Mats
    Yep, it's perfectly allowable, though often rather unexpectedly to many people. I always use the extra comma. It makes a few things easier. E.g. when you add another line to it then because you don't need to add a comma to the previous line then the change shows up as only an addition of one line in differencing programs like WinMerge.
    Then when it comes to constructor initialisation lists, doing the same thing is not allowed. Some times you gotta love the inconsistencies of C++.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM