0

I have two function calling inside C++ STL max function. Which function will be called first ?First one or Second one?

 max(rec(a,a[lev+1],lev+1,n,ar),rec(a,(a[lev]+a[lev+2])/2,lev+1,n,ar));
        
Roman
  • 23
  • 7
  • It's implementation defined, IIRC. – Some programmer dude Oct 29 '21 at 10:09
  • Can you please elaborate your answer? – Roman Oct 29 '21 at 10:10
  • What do you mean by implementation defined . – Roman Oct 29 '21 at 10:12
  • 1
    The "implementation" is the compiler, and it might do whatever it pleases when it evaluates function arguments. It might not even have a specific fixed and set order, for some calls it could do it in one order, for some other in a completely different order. – Some programmer dude Oct 29 '21 at 10:18
  • 1
    @Someprogrammerdude: "Implementation defined" **does** mean it has to be in the documentation, so it would be rather tricky to document a varying order. But since C++17 it's actually _indeterminately sequenced_. It doesn't need to be documented. – MSalters Oct 29 '21 at 11:55

1 Answers1

2

The only requirement is that there is an order. Once the compiler starts evaluating one argument, it has to finish that before it starts evaluating the other argument.

But the optimizer in the compiler may pick on a case-by-case basis what it thinks is best, so you cannot predict up front what choice the compiler will make.

MSalters
  • 173,980
  • 10
  • 155
  • 350