473,408 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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?
Jul 22 '05 #1
3 1280

"Derek" <no**@none.com> wrote in message news:br*************@ID-46268.news.uni-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.
Jul 22 '05 #2
On Tue, 9 Dec 2003 12:04:45 -0500, "Derek" <no**@none.com> 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.
Jul 22 '05 #3
> 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;
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
13
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.