comma at end of intialization list

This is a discussion on comma at end of intialization list within the C++ Programming forums, part of the General Programming Boards category; If we have a piece of code like this: Code: int arr[] = { 1, 2, #if SOMETHING 3, #endif ...

  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
    21,744
    Yes, it is permitted by the C++ Standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,304
    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, 03: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, 01: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

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