473,320 Members | 1,572 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,320 software developers and data experts.

precedence

the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data structure. My
question is, why aren't the brackets evaluated first then the dot operator
after since the brackets are higher on the precedence chart? Thanks
Jul 22 '05 #1
9 1946

"Andy White" <br***********@msn.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...
the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data
structure. My
question is, why aren't the brackets evaluated first then the dot
operator
after since the brackets are higher on the precedence chart? Thanks


Er... The content of the braces _is_ evaluated first:

long fkt(void)
{
printf("fkt");
return 0;
}

class FOO
{
public:
long& B(){printf("access B "); return b;}
long b;
};

FOO foo[100];
foo.B()[fkt()] = 7;
Jul 22 '05 #2
In article <10*************@corp.supernews.com>, Andy White
<br***********@msn.com> writes
the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data structure. My
question is, why aren't the brackets evaluated first then the dot operator
after since the brackets are higher on the precedence chart? Thanks


Because you start from the name, in this case 'foo' The subscript
operator is not appropriate to foo, and b isn't a free variable but one
that is identified in the context of foo.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 22 '05 #3

"Andy White" <br***********@msn.com> wrote in message
news:10*************@corp.supernews.com...
the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data structure. My
question is, why aren't the brackets evaluated first then the dot operator
after since the brackets are higher on the precedence chart? Thanks


A '[]' operator does not have higher precedence than a '.' operator. They
have the SAME precedence.

Regards,
Sumit.

Jul 22 '05 #4
Andy White wrote:
the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data structure. My
question is, why aren't the brackets evaluated first then the dot operator
after since the brackets are higher on the precedence chart? Thanks


(a) because precedence doesn't determine order of evaluation - it determines
what expressions are operands of what operators.

(b) the order of evaluation of the operands of [] is not specfied in
C [dunno about C++, I suspect not].

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Jul 22 '05 #5
Andy White wrote:
the statement:

foo.b[0] = 7;

indicates the first element of the array member of the data structure. My
question is, why aren't the brackets evaluated first then the dot operator
after since the brackets are higher on the precedence chart? Thanks


Firstly, C++ language does not have a "precedence chart". It has a
grammar, which also happens to determine what we usually refer to as
operator precedence and associativity. And according to C++ grammar, the
above expression should be interpreted as operator '[]' applied to
'foo.b'. Any "precedence charts" are noting more than attempts to more
or less closely represent operator relationship determined by C++
grammar in linear (more readable) form. You just found an example where
the chart you are using doesn't work very well.

Secondly, operator precedence and associativity defines grouping of
operators with their operands? but it doesn't define in any way the
order of evaluation of these operators. Hence, you question about
something being (or not being) "evaluated first" (and/or "evaluated
after") doesn't make much sense in this context.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #6
In article <10*************@news.supernews.com>, Andrey Tarasevich
<an**************@hotmail.com> writes
Secondly, operator precedence and associativity defines grouping of
operators with their operands? but it doesn't define in any way the
order of evaluation of these operators. Hence, you question about
something being (or not being) "evaluated first" (and/or "evaluated
after") doesn't make much sense in this context.


OK, we have a problem linguistic problem here. Expressions are evaluated
and neither C nor C++ specify the order of evaluation of
sub-expressions. However both provide rules about how operators are
sequenced. The grammar largely specifies the order in which operators
are to be handled. For example

x = a*b*c/d;

allows any order for obtaining values from a, b, c and d but requires
that a*b be evaluated prior to the evaluation of that result * c which
must happen prior to the evaluation of that expression/d.

Where it get complicated is when we have for example:

y = (a+b) * (c+d);
Now either a+b or c+d can be evaluated first because there is no
ordering applied to multiple instances of expressions in parentheses.

Clear as mud to most people. Basically do not write code where the order
in which the sub-expressions are evaluated makes any difference to the
result.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 22 '05 #7
Francis Glassborow wrote:
Secondly, operator precedence and associativity defines grouping of
operators with their operands? but it doesn't define in any way the
order of evaluation of these operators. Hence, you question about
something being (or not being) "evaluated first" (and/or "evaluated
after") doesn't make much sense in this context.
OK, we have a problem linguistic problem here. Expressions are evaluated
and neither C nor C++ specify the order of evaluation of
sub-expressions. However both provide rules about how operators are
sequenced. The grammar largely specifies the order in which operators
are to be handled. For example

x = a*b*c/d;

allows any order for obtaining values from a, b, c and d but requires
that a*b be evaluated prior to the evaluation of that result * c which
must happen prior to the evaluation of that expression/d.


No, it does not require that.

It is worth noting that it is difficult to come to a definite answer in
this case, because we are taking about built-in operators and there's no
way to detect the order in which built-in operators are invoked by the
implementation. For this very reason I don't like the notion of "order
of evaluation" applied to built-in operators.

Grouping between operand and operators ultimately defines the _result_
of the expression, but not the order of evaluation of its
subexpressions. The implementation is free to choose absolutely any
method of evaluation that leads to the correct result. In the above case
the compiler is free to start with evaluating, say, 'b/d' if it knows
how to ensure that the final value in 'x' is correct.

It is worth noting that in many cases order of evaluation is tied to the
correctness of the result much more than it would be in abstract
mathematics (like in situations when intermediate subexpressions can
produce overflows, precision loss etc). In such cases evaluating the
expression in "canonical" order (defined by precedence and
associativity) is the most straightforward way to ensure that the result
is correct. But, once again, the compiler is not required to do it this way.
Where it get complicated is when we have for example:

y = (a+b) * (c+d);
Now either a+b or c+d can be evaluated first because there is no
ordering applied to multiple instances of expressions in parentheses.
This expression can also be evaluated as the sum of 'a*c', 'a*d', 'b*c'
and 'b*d', if the compiler chooses to so for some reason and is capable
of ensuring that the final result is correct.
Clear as mud to most people. Basically do not write code where the order
in which the sub-expressions are evaluated makes any difference to the
result.


Yes, as long as there are no side effects. Side effect make things much
more complicated. I've seen people, who'd erroneously apply the above
rule and come to the conclusion that the following expression is OK,
because it "doesn't depend on the order of evaluation":

const int a[] = { 1, 2, 3 };
const int* p = a;

int sum = *p++ + *p++ + *p++; // <- this one

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #8
Andrey Tarasevich wrote:
[...]
It is worth noting that in many cases order of evaluation is tied to the
correctness of the result much more than it would be in abstract
mathematics (like in situations when intermediate subexpressions can
produce overflows, precision loss etc). In such cases evaluating the
expression in "canonical" order (defined by precedence and
associativity) is the most straightforward way to ensure that the result
is correct. But, once again, the compiler is not required to do it this way.
Pardon the interruption...

I know that "notes" are not normative, but 1.9/15, while being a note,
does talk about the compiler being prohibited from rewriting expressions
in such way that it might produce an exception in certain cases.

Is this what you mean by "not required"? And if it's not really required,
why does the Standard contain the words "the implementation cannot rewrite
this expression"?
[...]


Victor
Jul 22 '05 #9
Victor Bazarov wrote:
[...]
It is worth noting that in many cases order of evaluation is tied to the
correctness of the result much more than it would be in abstract
mathematics (like in situations when intermediate subexpressions can
produce overflows, precision loss etc). In such cases evaluating the
expression in "canonical" order (defined by precedence and
associativity) is the most straightforward way to ensure that the result
is correct. But, once again, the compiler is not required to do it this way.


Pardon the interruption...

I know that "notes" are not normative, but 1.9/15, while being a note,
does talk about the compiler being prohibited from rewriting expressions
in such way that it might produce an exception in certain cases.

Is this what you mean by "not required"? And if it's not really required,
why does the Standard contain the words "the implementation cannot rewrite
this expression"?
[...]


When used the term "result" in my previous message, I used it in its
broader meaning than the one assumed in C++ standard. My mistake. What I
really meant might be best referred to as, say, "outcome" of the
evaluation process, which includes obtaining result as well as, for
example, generating (or not generating) the exception mentioned in the note.

I don't argue that one elegant way to describe the evaluation of
expressions in C++ is to use the "as if" rule, i.e. to say that
expressions are evaluated "as if" the exact order of evaluation is
defined by operator precedence and associativity. The only problem I see
with it is that often in practical situations the meaning of "as if" in
the above sentence gets misunderstood or lost completely.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #10

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

Similar topics

10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
25
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
11
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
25
by: August Karlstrom | last post by:
Hi, Can someone explain the reason for the warning from GCC below: <shell-session> $ cat test.c #include <stdbool.h> bool a, b, c, d;
36
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
7
by: lovecreatesbea... | last post by:
c-faq, 4.3: The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. BSD Manual, OPERATOR(7): Operator Associativity...
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.