Order of evaluation: int a=1, b=a+1;

Discussion in 'C++' started by Derek, Dec 9, 2003.

  1. Derek

    Derek Guest

    Am I correct in assuming there is no guaranteed order of
    initialization for a and b in the statement

    int a=1, b=a+1;

    In other words, b=a+1 could be evaluated before a=1, right?
    Derek, Dec 9, 2003
    #1
    1. Advertising

  2. Derek

    Ron Natalie Guest

    "Derek" <> wrote in message news:br4vbe$27oo9t$-berlin.de...
    > Am I correct in assuming there is no guaranteed order of
    > initialization for a and b in the statement
    >
    > int a=1, b=a+1;
    >
    > In other words, b=a+1 could be evaluated before a=1, right?
    >

    Well, they are initialzed in sequence. You have to realize you don't b=a+1 isn't an
    expression, it's another init-declearator. If it were treated as an independent entity,
    you wouldn't even be assured that a was declared in the stuff to the right of the ,.

    Read the first few paragraphs of chapter 8 of the standard.
    Ron Natalie, Dec 9, 2003
    #2
    1. Advertising

  3. Derek

    Dan W. Guest

    On Tue, 9 Dec 2003 12:04:45 -0500, "Derek" <> wrote:

    >Am I correct in assuming there is no guaranteed order of
    >initialization for a and b in the statement
    >
    >int a=1, b=a+1;
    >
    >In other words, b=a+1 could be evaluated before a=1, right?
    >


    That's right; no guarantee.

    When I have constants that depend on other constants spread about, I
    try to write functions instead. The Eiffel language has a neat
    solution for this, called 'once' functions, which only compute a
    result and evaluate arguments once --the first time they are called;
    and from there on after always return the same result without
    computing anything. I've been thinking for longer than I care to
    compute, about a C++ implementation of lazy initialization and once
    functions, but can't find an elegant way, to this day.

    Might go something like,

    template< typename T, (T fun)() >
    struct once
    {
    typedef < typename T, (T fun)() > once_t;
    T *presult;
    once() : presult(0) {}
    T operator()()
    {
    if( presult ) return *presult;
    presult = new T( fun() );
    }
    };

    And repeat for single and multiple, const and non-const arguments for
    fun.
    Dan W., Dec 9, 2003
    #3
  4. > Am I correct in assuming there is no guaranteed order of
    > initialization for a and b in the statement
    >
    > int a=1, b=a+1;
    >
    > In other words, b=a+1 could be evaluated before a=1, right?


    No, you're not correct. This declaration is equivalent to

    int a = 1;
    int b = a+1;
    Andrew Koenig, Dec 9, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    390
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    752
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    333
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page