...or does it violate the rule that a variable may not be modified more
than once between two sequence points?
int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
- Eric 7 1524
Eric Lilja wrote:
..or does it violate the rule that a variable may not be modified more
than once between two sequence points?
int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
It's undefined.
Eric Lilja wrote:
..or does it violate the rule that a variable may not be modified more
than once between two sequence points?
There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.
int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
- Eric
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
Eric Lilja wrote:
..or does it violate the rule that a variable may not be modified more
than once between two sequence points?
There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.
That's not true
int n = 0;
std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.
--
Erik Wikström
--
Erik Wikström
* Erik Wikström:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>Eric Lilja wrote:
>>..or does it violate the rule that a variable may not be modified more than once between two sequence points?
There's a sequence point before each function call (i.e. before each << function), so that rule doesn't apply. But the order of evaluation of arguments is unspecified, so you can't count on any particular order for the various values of n.
That's not true
What isn't true?
>>int n = 0; std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.
Arguments can be evaluated before anything else happens.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Erik Wikström wrote:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>Eric Lilja wrote:
>>..or does it violate the rule that a variable may not be modified more than once between two sequence points?
There's a sequence point before each function call (i.e. before each << function), so that rule doesn't apply. But the order of evaluation of arguments is unspecified, so you can't count on any particular order for the various values of n.
That's not true
>>int n = 0; std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.
The order of the calls to operator << is well defined, but the order of
evaluation of the arguments is unspecified. The compiler can compute the
value of the first ++n, then the n, then the final ++n. It can also do
them in reverse order, or any other order.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
Pete Becker wrote:
Eric Lilja wrote:
>..or does it violate the rule that a variable may not be modified more than once between two sequence points?
There's a sequence point before each function call (i.e. before each <<
function), so that rule doesn't apply. But the order of evaluation of
arguments is unspecified, so you can't count on any particular order for
the various values of n.
That rule does apply. Since the function parameters may be evaluated
in any order, both ++n subexpressions may be executed before ANY
function call occurs. Hence you will have the undefined behavior or
twice modifying a value between sequence points. The rule requires
that be true for ANY ALLOWABLE ORDERING of the expression.
Erik Wikström wrote:
On Mar 1, 1:30 pm, Pete Becker <p...@versatilecoding.comwrote:
>Eric Lilja wrote:
>>..or does it violate the rule that a variable may not be modified more than once between two sequence points?
There's a sequence point before each function call (i.e. before each << function), so that rule doesn't apply. But the order of evaluation of arguments is unspecified, so you can't count on any particular order for the various values of n.
That's not true
Partially true.
>
>>int n = 0; std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
^^^^^^^^^^^^^^^^^
This has to execute before the << ' ' can, otherwise there won't be a
ostream as first parameter to the << ' '-call.
The real issue is that the compiler is free to execute both ++n
subexpressions in any order and at any time prior to the call
of the operator<< where they are parameters. Specifically it
can execute them BOTH before either operator << call ,causing
undefined behavior. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Zatvornitskiy |
last post by:
Hello All!
I'am novice in python, and I find one very bad thing (from my point of view) in
language. There is no keyword or syntax to declare variable, like 'var' in
Pascal, or special syntax in...
|
by: Rick |
last post by:
Hi,
Does deleting an object more than one times incur undefined behavior? I
think it doesn't but just making sure... thanks
Rick
|
by: Paminu |
last post by:
In math this expression:
(a < b) && (b < c)
would be described as:
a < b < c
But why is it that in C these two expressions evaluate to something
different for the same values of a, b and...
|
by: dei1c3 |
last post by:
I've created a WebUserControl which is Composite Control. It includes
several controls including a few RangeValidator controls and a
ValidationSummary control. The WebUserControl works properly...
|
by: Kiuhnm |
last post by:
I am having trouble with the function Add4Ptrs_v2.
I thought "twice< add_pointer<_>, _ >" was a perfectly valid
metafunction, but the following code does not work:
>>>>
#include <iostream>
...
| |
by: Sourav |
last post by:
Suppose I have a code like this,
#include <stdio.h>
int *p;
void foo(int);
int main(void){
foo(3);
printf("%p %d\n",p,*p);
|
by: Eric Lilja |
last post by:
I'm looking at an assignment where the students are expected to write a
class with a given purpose. According to the assignment, you should be
able to do this with instances of the class:...
|
by: onkar |
last post by:
Given the following code & variable i .
int main(int argc,char **argv){
int i;
printf("%d\n",i);
return 0;
}
here i is allocated from bss or stack ?
|
by: paresh |
last post by:
Is this the valid C statement.
int a,b,c;
c = 5;
<<<
a = b = c;
Can anyone throw the light on this.
-Paresh
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |