Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order of evaluation: int a=1, b=a+1;

Reply
Thread Tools

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

 
 
Derek
Guest
Posts: n/a
 
      12-09-2003
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?


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      12-09-2003

"Derek" <> wrote in message news:br4vbe$27oo9t$...
> 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.


 
Reply With Quote
 
 
 
 
Dan W.
Guest
Posts: n/a
 
      12-09-2003
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.
 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      12-09-2003
> 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;


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57