Consider these two approaches that can represent an \"optional int\":
using std_optional_int = std::optional;
using my_optional_int =
Why does
get_std_optional_int()require threemovinstructions, whileget_my_optional()only needs a singlemovabs?
The direct cause is that optional is returned through a hidden pointer while pair is returned in a register. Why is that, though? The SysV ABI specification, section 3.2.3 Parameter Passing says:
If a C++ object has either a non-trivial copy constructor or a non-trivial destructor, it is passed by invisible reference.
Sorting out the C++ mess that is optional is not easy, but there seem to be a non-trivial copy constructor at least in the optional_base class of the implementation I checked.