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

pre, post increment standard behaviour, and friend function declaration

while playing around to check <blah> for my personal project, I discovered
an anomaly with my code, compiled using (cringe) visual studio ver 6:
#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5

while

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a result
of the chained function calls?

And another question:
Q: Is it possible to declare the constructor of class B as a friend function
to class A, and if so what is the syntax?

thanks in advance,
ed.
Jul 22 '05 #1
18 1780
eddiew_AUS wrote:
while playing around to check <blah> for my personal project,
What is "<blah>"?
I discovered an anomaly with my code, compiled using (cringe) visual
studio ver 6:
#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5
I don't see why you would expect that.

while

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a
result of the chained function calls?
It's the result of changing k's value multiple times without a sequence
point in between. The behaviour of that code is undefined.
And another question:
Q: Is it possible to declare the constructor of class B as a friend
function to class A, and if so what is the syntax?


I don't think so. Constructors don't have a name.

Jul 22 '05 #2
eddiew_AUS wrote:

int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5
There is no expected output, since the above has
undefined behaviour

Q: Is this another example of non-standard microsoft compilers, or a result
of the chained function calls?
It is a possible result of undefined behaviour. Please check the
groups archive at google. This topic has been beaten to death.

And another question:
Q: Is it possible to declare the constructor of class B as a friend function
to class A, and if so what is the syntax?


No it is not. Constructors don't have names.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
eddiew_AUS wrote:

int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5


There is no expected output, since the above has
undefined behaviour

Q: Is this another example of non-standard microsoft compilers, or a result of the chained function calls?


It is a possible result of undefined behaviour. Please check the
groups archive at google. This topic has been beaten to death.

And another question:
Q: Is it possible to declare the constructor of class B as a friend function to class A, and if so what is the syntax?


No it is not. Constructors don't have names.

--
Karl Heinz Buchegger
kb******@gascad.at


Thanks for the replies. I now understand the reasons behind both answers. :/

cheers, ed.
Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv************@news.t-online.com...
eddiew_AUS wrote:
while playing around to check <blah> for my personal project,


What is "<blah>"?


<blah> is a simple game with which i am challenging my skills at using and
understanding 3rd party code, increasing my understanding of the C++
language and especially the STL, and helping my good programming practices
(spelt with an s or a c??) with a copy of the C++ FAQs by my keyboard.

the object of <blah> is to negotiate a 3 dimensional tunnel at speed. Tunnel
is created using concepts from random walk functions at run-time, with
randomness apparent in the shape of the wall of the tunnel and the
approximate centre of the tunnel.

Not really that relevant to a technical question, but ask away and i'll be
more than happy to discuss any other part of this project :)

cheers, ed.
Jul 22 '05 #5
Rolf Magnus wrote:
eddiew_AUS wrote:

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a
result of the chained function calls?

It's the result of changing k's value multiple times without a sequence
point in between. The behaviour of that code is undefined.


Not true. There are a number of sequence points (before each function
call and return). It's unspecified behavior due to the order of
evaluation of the function parameters.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6
Karl Heinz Buchegger wrote:
eddiew_AUS wrote:
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5

There is no expected output, since the above has
undefined behaviour


Unspecified, but not undefined. At least according to the discussion of
a nearly identical program a week or two ago. There are sequence points
before each function call and return.

The problem, I think, is that in

a.b(c);

it is not specified whether 'a' or 'c' is evaluated first. But I haven't
located the exact section of the standard that says this.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #7
Kevin Goodsell wrote:
Rolf Magnus wrote:
eddiew_AUS wrote:

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a
result of the chained function calls?

It's the result of changing k's value multiple times without a
sequence point in between. The behaviour of that code is undefined.


Not true. There are a number of sequence points (before each function
call and return). It's unspecified behavior due to the order of
evaluation of the function parameters.


I never get that right.

Jul 22 '05 #8
Kevin Goodsell wrote:
...
The problem, I think, is that in

a.b(c);

it is not specified whether 'a' or 'c' is evaluated first. But I haven't
located the exact section of the standard that says this.
...


5/4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions [...] is
unspecified [...]

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #9
Andrey Tarasevich wrote:

5/4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions [...] is
unspecified [...]


The example in that section has me confused. I'm looking at a draft (Nov
1997), so maybe it's different in the final:

i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented

The two statements that are listed as unspecified I think should be
undefined.

Can anyone tell me if this example has been changed? And isn't it true
that the behavior is undefined, not unspecified?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #10
Kevin Goodsell wrote:

5/4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions [...] is
unspecified [...]


The example in that section has me confused. I'm looking at a draft (Nov
1997), so maybe it's different in the final:

i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented

The two statements that are listed as unspecified I think should be
undefined.

Can anyone tell me if this example has been changed? And isn't it true
that the behavior is undefined, not unspecified?


Yes, the behavior is undefined. The example in 5/4 is defective, as
reported in

http://anubis.dkuug.dk/JTC1/SC22/WG2...ctive.html#351

In the OP's case the behavior is still unspecified for the reasons that
you correctly stated in your previous messages.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #11
Andrey Tarasevich wrote:
Kevin Goodsell wrote:

Can anyone tell me if this example has been changed? And isn't it true
that the behavior is undefined, not unspecified?

Yes, the behavior is undefined. The example in 5/4 is defective, as
reported in

http://anubis.dkuug.dk/JTC1/SC22/WG2...ctive.html#351

In the OP's case the behavior is still unspecified for the reasons that
you correctly stated in your previous messages.


Excellent. Thank you.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #12

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:_p*****************@newsread2.news.pas.earthl ink.net...

The example in that section has me confused. I'm looking at a draft (Nov
1997), so maybe it's different in the final
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented

The two statements that are listed as unspecified I think should be
undefined.
You are correct.

Can anyone tell me if this example has been changed? And isn't it true
that the behavior is undefined, not unspecified?

No it's still wrong, even after TC1. Note that the examples do not have any meaning
for defining the language (non-normative).

Jul 22 '05 #13
On Mon, 26 Jan 2004 20:07:39 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c++:
Rolf Magnus wrote:
eddiew_AUS wrote:

#include<iostream>

using namespace std;

int main( void )
{
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " " << k << "\n";
}

produced the output of
3 4 4 4 3

Q: Is this another example of non-standard microsoft compilers, or a
result of the chained function calls?

It's the result of changing k's value multiple times without a sequence
point in between. The behaviour of that code is undefined.


Not true. There are a number of sequence points (before each function
call and return). It's unspecified behavior due to the order of
evaluation of the function parameters.


May or may not be true. There is absolutely nothing in the standard
that prohibits the evaluation of all four terms involving k before
making any calls to the cout insertion operator. In that case the
behavior is undefined.

So depending on the unspecified order of evaluation of the arguments,
it might be undefined behavior, or merely unspecified behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #14
On Mon, 26 Jan 2004 20:17:58 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c++:
Karl Heinz Buchegger wrote:
eddiew_AUS wrote:
int k = 3;
cout << k;
cout << " " << k++ << " " << k << " " << ++k << " ";
cout << k << "\n";
}

produces the expected output of:
3 4 4 4 5

There is no expected output, since the above has
undefined behaviour


Unspecified, but not undefined. At least according to the discussion of
a nearly identical program a week or two ago. There are sequence points
before each function call and return.


No, honestly, it is unspecified as to whether it is undefined or not.
There is nothing in the standard that prevents an implementation from
evaluating all terms involving "k" before calling any of the operator
functions. If an implementation does so, "k" is modified twice
between sequence points, and its value is also accessed two other
times in a manner not involved with calculating the new value. In
this case, then, the behavior is undefined.

On the other hand, if a compiler evaluates each term involving "k"
just before calling the cout insertion operator function, it has
unspecified behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #15
Jack Klein wrote:
On Mon, 26 Jan 2004 20:07:39 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c++:

Not true. There are a number of sequence points (before each function
call and return). It's unspecified behavior due to the order of
evaluation of the function parameters.

May or may not be true. There is absolutely nothing in the standard
that prohibits the evaluation of all four terms involving k before
making any calls to the cout insertion operator. In that case the
behavior is undefined.

So depending on the unspecified order of evaluation of the arguments,
it might be undefined behavior, or merely unspecified behavior.


Good point. That didn't even occur to me. Thanks for pointing it out.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #16
Kevin Goodsell wrote:

Karl Heinz Buchegger wrote:

Unspecified, but not undefined.
You are right.
At least according to the discussion of
a nearly identical program a week or two ago. There are sequence points
before each function call and return.

The problem, I think, is that in

a.b(c);

it is not specified whether 'a' or 'c' is evaluated first. But I haven't
located the exact section of the standard that says this.


You are searching for
5.2.2-1
and
5.2.2-8

For a discussion of this, see eg.
http://groups.google.at/groups?q=buc...scad.at&rnum=4

This post was written by, aehm, me.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #17

"Jack Klein" <ja*******@spamcop.net> wrote in message news:1m********************************@4ax.com...
No, honestly, it is unspecified as to whether it is undefined or not.
There is nothing in the standard that prevents an implementation from
evaluating all terms involving "k" before calling any of the operator
functions. If an implementation does so, "k" is modified twice
between sequence points, and its value is also accessed two other
times in a manner not involved with calculating the new value. In
this case, then, the behavior is undefined.


Nope, the behavior is simply undefined. The rules say that if the value is
modified twice between sequence point for ANY ALLOWABLE ORDERING
of operations, then the behavior is undefined.

Jul 22 '05 #18
Andrey Tarasevich wrote:
...
In the OP's case the behavior is still unspecified for the reasons that
you correctly stated in your previous messages.
...


Actually, Jack Klein is right - its undefined. I've seen the reasoning
he uses in his message many times and even used it myself, but somehow I
managed to forget about it completely... :)

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #19

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

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
9
by: Neelesh | last post by:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends") where it is mentioned that What I observe is a bit different - it is fine that there is an error for Xform, but the reason...
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.