473,698 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1958

"Andy White" <br***********@ msn.com> schrieb im Newsbeitrag
news:10******** *****@corp.supe rnews.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("acc ess B "); return b;}
long b;
};

FOO foo[100];
foo.B()[fkt()] = 7;
Jul 22 '05 #2
In article <10************ *@corp.supernew s.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.supe rnews.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.supernew s.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
3309
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 looked at the grammar in the C++ standard. The relative fragments are as follows: conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression
25
1706
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 (fastIoDispatch->FastIoRead)( FileObject, FileOffset, Length, Wait,
21
3030
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 opposed to what is mentioned in precedence table? Also, with comma operator. consider,
11
1549
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
2320
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
2795
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
1786
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 -------- ------------- ! ~ ++ -- - (type) * & sizeof right to left Is there a complete table on operators, their precedence and
5
2538
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
3745
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'); printf("%d", a); /* code end */ 2
7
1844
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
8603
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
9027
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...
0
7725
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...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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
4369
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...
1
3046
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
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.