473,595 Members | 2,623 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 1951
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
2668
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 + 1
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
2135
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
3338
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
4103
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
4687
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 mapped register that only has a select number of writable bits. He claims it has been a 'C...
20
1602
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
1562
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 is the same
54
3901
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
7955
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8379
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6674
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5418
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1223
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.