0
How to stop constructor call from max with three objects
Hi Please refer code below: https://sololearn.com/compiler-playground/cC9dwnGpLCRR/?ref=app It has copy constructor calls. With max called two times with two objects , copy constructor is not needed. Do we have some option to remove avoid copy constructors for max with three objects?
5 Antworten
+ 1
don't use initializer list if you don't want copies and use the nested max you commented out.
max(obj1, max(obj2, max(obj3, max(...max(objn))
Unless there's a crazy amount of objects you're processing, is there really a need for obsessive optimization?
Initializer list is readable and easy to extend for more objects.
+ 1
maybe this?
https://stackoverflow.com/questions/17803475/why-is-stdinitializer-list-often-passed-by-value
anyway, writing a custom variadic function is not that hard..
https://sololearn.com/compiler-playground/cYi8m4MfJdCX/?ref=app
+ 1
Thanks Bob_Li .. it's not about variadic or max used two times for three objects. I can always use two max for obj1 and obj2 and compare that output with obj3.
Initializer list is from standard c++ functions and that makes me curious how come standard libraries are not having efficient functions for simply finding max values from objects
0
That's correct. We do have normal approach of comparing two objects at a time, but why not the initializer list is not optimized one
0
bad decisions. C++ is full of them... it's handicapped because it needs to support backward compatibililty... Also, decisions are made by a committee, and people as a group tends to be dumber than individuals...
https://www.reddit.com/r/cpp/comments/7a1o7f/why_does_stdinitializer_list_prevents_using/
https://stackoverflow.com/questions/8193102/initializer-list-and-move-semantics/43025111#43025111
Then there are projects that insists on starting with ancient C++ standards instead of upgrading to newer ones. So it's moving with one foot stuck in cement.