There are subtle differences:
Function1
requires non explicit A
's constructor.
construct A
"in place" (even before C++17). so copy/move A's constructor might even be marked as = delete
, and program is well formed.
pre-C++17, Function2
will use move/copy constructor which might be elided (return value optimization: RVO) (copy/move constructor should be available though (marking as = delete
won't compile)).
Post C++17, no move/copy are done (similar to Function1
, but work with explicit
constructors).
Function3
will use move/copy constructor which might be elided (named return value optimization: NRVO).
With correct options, compilers will do the optimization, and all should be equivalent.
With options such as -fno-elide-constructors
, pre-c++17, Function1
should win, post C++17, Function1
and Function2
should win.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…