Thread: Warnings about initializer order

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Warnings about initializer order

    I get many warnings about the order of initializers in my classes. If I initialize them in some order other than that in which I have listed them in the class, the compiler warns that they actually will be initialized in the latter order. I can only imagine that this warning is to help prevent the initialization of one member before another on which it depends.

    That being said, I always thought that it was bad practice (even undefined) to have members whose initialization depends on each other. Is this not so?

    I need to work on my English.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well there it is. Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Strictly speaking, there is no requirement to initialise class members in the order they are declared. It is in some style guides, ironically enough, because some compilers issue warnings about such things. The order of initialisation is actually implementation defined - which is why it is not a good idea to have member initialisation able to be affected by order of initialisation.

    The standard is, specific on the order in which base classes are initialised, and that order is unaffected by how the programmer orders initialisation lists.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    Strictly speaking, there is no requirement to initialise class members in the order they are declared. It is in some style guides, ironically enough, because some compilers issue warnings about such things. The order of initialisation is actually implementation defined - which is why it is not a good idea to have member initialisation able to be affected by order of initialisation.
    That seems to contradict paragraph 5 of section 12.6.2 of the 2003 edition of the C++ Standard:
    Initialization shall proceed in the following order:
    — First, and only for the constructor of the most derived class as described below, virtual base classes shall be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where "left-to-right" is the order of appearance of the base class names in the derived class base-specifier-list.
    — Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
    — Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
    — Finally, the body of the constructor is executed.
    [Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. ]
    The third point defines the order of initialisation as being the order they were declared.
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hmmm ..... I agree with your interpretation, laserlight.

    I had a bit of now-ancient history in the back of my mind: some drafts of the standard (well before ratification in '98) left the order of initialisation of non-static members implementation-defined. One of the joys of working with C++ in the 90s is that some things were debated and later adopted. Having participated in discussions of such things at the time and, more recently, having to help people forced to use old compilers, the memories still emerge.

    However, compilers are not required to give warnings if initialisers are ordered differently.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 03:22 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. constructor's initializer list: order of evaluation
    By rpet in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 06:21 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM