Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't c++ have a specified order for evaluating function arguements?

Tags:

c++

c++11

c++14

It seems to me that it's a very basic and necessary feature of any functional programming language to know the order in which the arguments of a function call will be evaluated. Am I wrong in this? Why doesn't C++ define this? Is it being discussed for any future version of C++?

like image 257
jDogg Avatar asked Aug 04 '16 10:08

jDogg


2 Answers

Why doesn't C++ do it this way?

For starters, C++ isn't a functional programming language.

And the fastest way to evaluate the arguments depends on the implementation and architecture, so the compiler gets to choose. We don't pay for what we don't use, in C++, so if you need to specify an evaluation order yourself then you can do so explicitly with named variables.

Although, continuing the theme of newer standards leaving behind sacred C++ paradigms, C++17 will sadly add some evaluation order guarantees, ruining all of that.

like image 78
Lightness Races in Orbit Avatar answered Oct 24 '22 19:10

Lightness Races in Orbit


As of C++17 it guarantees arguments will be evaluated linearly and will not interleave but not in any specific order - https://channel9.msdn.com/Shows/C9-GoingNative/GoingNative-52-ISO-C-Oulu-Debriefing

The reason it didn't before was to allow compiler implementers the scope to optimise the order of evaluation, it turned out not to be used or to be used poorly to the extent that logical order can and will be enforced with negligible impact.

EDIT: correction, thanks @Oktalist

I actually think this is an odd decision, it seems obvious to me that interleaving is an easier optimisation than argument evaluation reordering and I don't think we're going to treat argument evaluation with any more trust than we did before.

like image 36
1stCLord Avatar answered Oct 24 '22 19:10

1stCLord