473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about evaluation order

Hi all,

I've got a very specific question about the evaluation order in C++.
Assume some kind of custom array class, with an overloaded subscript
operator. In the following code:

{
my_array a, b, c;

a[6] = b[5] + c[4];
}

I would assume the following order:

1) The subscript operators are evaluated for 'b' and 'c', both
returning a reference (double& for example)
2) The add-operator is called, returning a temporary object
3) The subscript operator is evaluated for 'a', returning a reference.
4) The = operator is evaluated.

My question: is this the way things go, or is this order not defined in
C++? To be more specific: is it possible that the subscript evaluation
for 'a' (step 3) is done already in step 1 (before the add-operator)?

Thanx,

Jeroen
Feb 28 '07 #1
15 1944
Jeroen wrote:
To be more specific: is it possible that the subscript evaluation
for 'a' (step 3) is done already in step 1 (before the add-operator)?
Yes.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 28 '07 #2
Pete Becker schreef:
Jeroen wrote:
>To be more specific: is it possible that the subscript evaluation for
'a' (step 3) is done already in step 1 (before the add-operator)?

Yes.
Thanks Pete! That keeps me from working out an implementation that
doesn't work in the end....
Feb 28 '07 #3

"Jeroen" <no*****@thanx. comwrote in message news:45e57267$1 @cs1...
Hi all,

I've got a very specific question about the evaluation order in C++.
Assume some kind of custom array class, with an overloaded subscript
operator. In the following code:

{
my_array a, b, c;

a[6] = b[5] + c[4];
}

I would assume the following order:

1) The subscript operators are evaluated for 'b' and 'c', both returning
a reference (double& for example)
2) The add-operator is called, returning a temporary object
3) The subscript operator is evaluated for 'a', returning a reference.
4) The = operator is evaluated.

My question: is this the way things go, or is this order not defined in
C++? To be more specific: is it possible that the subscript evaluation for
'a' (step 3) is done already in step 1 (before the add-operator)?
It's certainly possible that the subscript op of a is evaluated before the
right side of the assignment is taken care of. This would be required for
example to allow for optimizing the temporary created by + operator.

Just out of curiosity, why or rather in what sense would this have an impact
on your implementation of the subscript op.

Cheers
Chris
Feb 28 '07 #4
Chris Theis schreef:
Just out of curiosity, why or rather in what sense would this have an impact
on your implementation of the subscript op.

Cheers
Chris

I am working on a multidimensiona l matrix which allows subscripting
resulting in submatrices instead of single cells (just like Matlab does
if you are familliar with that). The problem however is that you cannot
pass a reference to that submatrix after evaluating the subscript
operator, because the submatrix cannot be represented by the original
matrix memory structure. And I don't want to return a temporary object
because that can cause tremendous overhead if the matrix is large, and
(if I am not mistaken) I cannot do correct assignemnts if the submatrix
'selection' is on the lhs (only the temporary submatrix object will be
assigned the new value!):

a("1:6") = 3;

So I thought that I could return a reference to the original matrix, but
in the class I save the subscript string as an indicator for the
selected submatrix. My operators then take care of that information.
That should work fine with:

a("1:6") = b("1:6") + c("1:6");

but we get a serious problem (what the original question was about) with:

a("1:6") = a("2:7") + a("11:16");

The subscript string as saved in the class is overwritten by sequential
evaluation of the subscript operator, and there's no way to get around
the problem like this.

In the meantime I think I have a solution that does work, but that's
another 'big' story. I have to work that out first and think of possible
complications. If I got questions, or a working matrix class available
on a webpage, I'll post agin!

Jeroen
Feb 28 '07 #5
"Jeroen" <no*****@thanx. comwrote in message news:45e5a5de$1 @cs1...
Chris Theis schreef:
>Just out of curiosity, why or rather in what sense would this have an
impact on your implementation of the subscript op.

Cheers
Chris

I am working on a multidimensiona l matrix which allows subscripting
resulting in submatrices instead of single cells (just like Matlab does if
you are familliar with that). The problem however is that you cannot pass
a reference to that submatrix after evaluating the subscript operator,
because the submatrix cannot be represented by the original matrix memory
structure. And I don't want to return a temporary object because that can
cause tremendous overhead if the matrix is large, and (if I am not
mistaken) I cannot do correct assignemnts if the submatrix 'selection' is
on the lhs (only the temporary submatrix object will be assigned the new
value!):

a("1:6") = 3;

So I thought that I could return a reference to the original matrix, but
in the class I save the subscript string as an indicator for the selected
submatrix. My operators then take care of that information. That should
work fine with:

a("1:6") = b("1:6") + c("1:6");

but we get a serious problem (what the original question was about) with:

a("1:6") = a("2:7") + a("11:16");

The subscript string as saved in the class is overwritten by sequential
evaluation of the subscript operator, and there's no way to get around the
problem like this.
Okay, now I see your point. Well, that's quite a tricky thing I gotta admit.
At first glance I'd guess that you'd have to sacrifice the syntax with the
subscript operator to have it working. But I'm curious to see what you will
pull off.

Cheers
Chris
Feb 28 '07 #6
Jeroen wrote:
Chris Theis schreef:
>Just out of curiosity, why or rather in what sense would this have an
impact on your implementation of the subscript op.

Cheers
Chris

I am working on a multidimensiona l matrix which allows subscripting
resulting in submatrices instead of single cells (just like Matlab does
if you are familliar with that). The problem however is that you cannot
pass a reference to that submatrix after evaluating the subscript
operator, because the submatrix cannot be represented by the original
matrix memory structure. And I don't want to return a temporary object
because that can cause tremendous overhead if the matrix is large, and
(if I am not mistaken) I cannot do correct assignemnts if the submatrix
'selection' is on the lhs (only the temporary submatrix object will be
assigned the new value!):

a("1:6") = 3;

So I thought that I could return a reference to the original matrix, but
in the class I save the subscript string as an indicator for the
selected submatrix. My operators then take care of that information.
That should work fine with:

a("1:6") = b("1:6") + c("1:6");

but we get a serious problem (what the original question was about) with:

a("1:6") = a("2:7") + a("11:16");

The subscript string as saved in the class is overwritten by sequential
evaluation of the subscript operator, and there's no way to get around
the problem like this.
The problem is your design, specifically your choice to embed this
active subscript within the matrix class. Perhaps a better approach is
to make a bona fide class to represent a submatrix. Give it a reference
to an underlying matrix, some information about its range in that
matrix, and overload it's array index and assignment operators to read
and write the underlying matrix. It may even make sense to define an
abstract matrix class and derive from it two classes, a concrete matrix
class which holds its data values, and a relative matrix class which
holds a reference and a range.

Mark
Feb 28 '07 #7
Jeroen wrote:
matrix memory structure. And I don't want to return a temporary object
because that can cause tremendous overhead if the matrix is large, and
(if I am not mistaken) I cannot do correct assignemnts if the submatrix
'selection' is on the lhs (only the temporary submatrix object will be
assigned the new value!):

a("1:6") = 3;

So I thought that I could return a reference to the original matrix, but
in the class I save the subscript string as an indicator for the
selected submatrix. My operators then take care of that information.
That should work fine with:

a("1:6") = b("1:6") + c("1:6");

but we get a serious problem (what the original question was about) with:

a("1:6") = a("2:7") + a("11:16");

The subscript string as saved in the class is overwritten by sequential
evaluation of the subscript operator, and there's no way to get around
the problem like this.
An intermediate solution with not so big overhead can be to use references
to the original matrix for the subscripts but creating new objects for the
result of operators.

--
Salu2
Feb 28 '07 #8
Mark P schreef:
Jeroen wrote:
>Chris Theis schreef:
>>Just out of curiosity, why or rather in what sense would this have an
impact on your implementation of the subscript op.

Cheers
Chris

I am working on a multidimensiona l matrix which allows subscripting
resulting in submatrices instead of single cells (just like Matlab
does if you are familliar with that). The problem however is that you
cannot pass a reference to that submatrix after evaluating the
subscript operator, because the submatrix cannot be represented by the
original matrix memory structure. And I don't want to return a
temporary object because that can cause tremendous overhead if the
matrix is large, and (if I am not mistaken) I cannot do correct
assignemnts if the submatrix 'selection' is on the lhs (only the
temporary submatrix object will be assigned the new value!):

a("1:6") = 3;

So I thought that I could return a reference to the original matrix,
but in the class I save the subscript string as an indicator for the
selected submatrix. My operators then take care of that information.
That should work fine with:

a("1:6") = b("1:6") + c("1:6");

but we get a serious problem (what the original question was about) with:

a("1:6") = a("2:7") + a("11:16");

The subscript string as saved in the class is overwritten by
sequential evaluation of the subscript operator, and there's no way to
get around the problem like this.

The problem is your design, specifically your choice to embed this
active subscript within the matrix class. Perhaps a better approach is
to make a bona fide class to represent a submatrix. Give it a reference
to an underlying matrix, some information about its range in that
matrix, and overload it's array index and assignment operators to read
and write the underlying matrix. It may even make sense to define an
abstract matrix class and derive from it two classes, a concrete matrix
class which holds its data values, and a relative matrix class which
holds a reference and a range.

Mark
The thing I came up with yesterday partly resembles your suggestion I guess:

class matrix {
private:
vector<double*d ata;
bool matrix_is_a_sub matrix;
submatrix_specs s;

// plus ctors, dtor, operators....
};

What I want to do is that ctors always generate a matrix object in which
'matrix_is_a_su bmatrix=false', so that object actually stores matrix
data. The subscript operators however return a temporary object of this
matrix class (not a reference), but with the specifications of the
selected submatrix stored in 's', 'matrix_is_a_su bmatrix=true' and
'data' points to the original matrix data (so I don't have to copy the
actual matrix data). My matrix operators then only have to check the
'matrix_is_a_su bmatrix' flag to decide on correct behaviour. This flag
is also used in the dtor to decide if the dtor should delete the matrix
data (only if 'matrix_is_a_su bmatrix=false'. I have to think about the
consequences of this approach, but I think it will work.

Jeroen
Mar 1 '07 #9
"Jeroen" <no*****@thanx. comwrote in message news:45e6952c$1 @cs1...
[SNIP]
>>
The problem is your design, specifically your choice to embed this active
subscript within the matrix class. Perhaps a better approach is to make
a bona fide class to represent a submatrix. Give it a reference to an
underlying matrix, some information about its range in that matrix, and
overload it's array index and assignment operators to read and write the
underlying matrix. It may even make sense to define an abstract matrix
class and derive from it two classes, a concrete matrix class which holds
its data values, and a relative matrix class which holds a reference and
a range.

Mark

The thing I came up with yesterday partly resembles your suggestion I
guess:

class matrix {
private:
vector<double*d ata;
bool matrix_is_a_sub matrix;
submatrix_specs s;

// plus ctors, dtor, operators....
};

What I want to do is that ctors always generate a matrix object in which
'matrix_is_a_su bmatrix=false', so that object actually stores matrix data.
The subscript operators however return a temporary object of this matrix
class (not a reference), but with the specifications of the selected
submatrix stored in 's', 'matrix_is_a_su bmatrix=true' and 'data' points to
the original matrix data (so I don't have to copy the actual matrix data).
My matrix operators then only have to check the 'matrix_is_a_su bmatrix'
flag to decide on correct behaviour. This flag is also used in the dtor to
decide if the dtor should delete the matrix data (only if
'matrix_is_a_su bmatrix=false'. I have to think about the consequences of
this approach, but I think it will work.
I agree that the approach will work but IMO it introduces unnecessary
complications because you have to check whether it's a submatrix or not etc.
I'd suggest to consider each matrix object as a kind of proxy, holding the
data as well as always a submatrix object. This submatrix object is keeping
track of the data indices that are referenced by the matrix. So this
embedded object is working as an interface to give you access to the data,
of whatever range you want to. You can think of this submatrix object as a
clever kind of subscript operator which filters out exactly the parts that
you request.

Cheers
Chris
Mar 1 '07 #10

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

Similar topics

11
2659
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 possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i...
5
1289
by: Juri | last post by:
Hi all, I have a question regarding the << operator. I wrote a simple queue class, which contains add(char), del() and empty() functions. They works fine. Now I want its output. If I write the following way, it gives me the correct output. while(!q1.empty()){ // This prints "abc" cout<<q1.del();
8
2131
by: gouqizi.lvcha | last post by:
I have a statement as follows, a = b++; why b=b+1 after a=b. I check the C language precedence (K&R Page 52) , ++ should has higher precedence than = . Rick
8
3333
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 || operator is before the && operator? e,g, In an expression like a = (z>x) || (x<0) && (z-y>9); is it guaranteed that z>x will be checked first?
21
4089
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; }
77
4672
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other ended up compiling it as a = c; b = c. (Both with no optimizations enabled). How did we notice you may ask? Well, in our case 'b' was a memory...
20
1585
by: mdh | last post by:
Could someone help me understand one aspect of an exercise in K&R II; (page 76) Given function: double pop( void){ ....return a double from a stack if present }
25
1558
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a general rule as to when this type of increment occurs? ( I understand that the increment in "c = ++*s" occurs immediately before assignment, so my guess...
54
3875
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 parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.