Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: C has no evaluation order of function parameters, am I missing something? Date: Wed, 14 Aug 2019 09:26:01 -0700 Organization: A noiseless patient Spider Lines: 59 Message-ID: <86r25n7lye.fsf@mailhub.linuxsc.com> References: <0fa0ca89-6e91-4f23-b025-74e50bb97646@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="f86338710750d7af41889a1532e7291c"; logging-data="23014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9oUdQ97KOVMXrDSrPK813PZGBp4lg0rc=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:SXyDuWL7uRZLwxzM/n3OIhkkFAc= sha1:+wUBF5gyqRC2TNqmZHcryLNniKM= Xref: csiph.com comp.lang.c++:67653 ram@zedat.fu-berlin.de (Stefan Ram) writes: > It has been written: > >> Subject: C has no evaluation order of function parameters > > main.c > > #include > #include > > 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 #include 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.