Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #67653
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: C has no evaluation order of function parameters, am I missing something? |
| Date | 2019-08-14 09:26 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86r25n7lye.fsf@mailhub.linuxsc.com> (permalink) |
| References | <0fa0ca89-6e91-4f23-b025-74e50bb97646@googlegroups.com> <UB-20190809182755@ram.dialup.fu-berlin.de> |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> It has been written:
>
>> Subject: C has no evaluation order of function parameters
>
> main.c
>
> #include <math.h>
> #include <stdio.h>
>
> double f( int const n )
> { printf( "%d\n", n );
> return 0; }
>
> int main( void )
> { pow( f( 0 ), f( 1 )); }
>
> For one example, the above might have undefined behavior,
No, it cannot.
> because
>
> |side effects and value computations of subexpressions are
> |unsequenced
>
> and we cannot know whether >>printf<< has any (internal)
> side-effects on a scalar object.
>
> |If a side effect on a scalar object is unsequenced relative
> |to either a different side effect on the same scalar object
> |or a value computation using the value of the same scalar
> |object, the behavior is undefined.
None of that matters because function calls are (at a minimum)
indeterminately sequenced. The calls 'f(0)' and 'f(1)' cannot
overlap: one must be evaluated completely before the other, even
though we don't know which is done first and which is done second.
Even if function calls were not indeterminately sequenced, in C++
this example still would not have undefined behavior. Take this
simple example (disclaimer: not compiled):
#include <math.h>
#include <stdio.h>
int
main(){
int x = 0;
double d = pow( x, ++x );
printf( "d is %f\n", d );
return 0;
}
In C this program has undefined behavior. In C++ it does not,
because the evaluations of function arguments are indeterminately
sequenced with respect to each other. There is unspecified
behavior, but not undefined behavior.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
C has no evaluation order of function parameters, am I missing something? silidrone@gmail.com - 2019-08-09 09:37 -0700
Re: C has no evaluation order of function parameters, am I missing something? gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-09 16:54 +0000
Re: C has no evaluation order of function parameters, am I missing something? silidrone@gmail.com - 2019-08-09 09:59 -0700
Re: C has no evaluation order of function parameters, am I missing something? gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-09 19:35 +0000
Re: C has no evaluation order of function parameters, am I missing something? Keith Thompson <kst-u@mib.org> - 2019-08-09 14:02 -0700
Re: C has no evaluation order of function parameters, am I missing something? silidrone@gmail.com - 2019-08-09 09:56 -0700
Re: C has no evaluation order of function parameters, am I missing something? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-09 10:24 -0700
Re: C has no evaluation order of function parameters, am I missing something? Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-09 19:27 +0200
Re: C has no evaluation order of function parameters, am I missing something? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-09 11:22 -0700
Re: C has no evaluation order of function parameters, am I missing something? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-14 09:26 -0700
csiph-web