Why is the construction of std::optional more expensive than a std::pair?

libstdc++ apparently does not implement P0602 “variant and optional should propagate copy/move triviality”. You can verify this with:

static_assert(std::is_trivially_copyable_v<std::optional<int>>);

which fails for libstdc++, and passes for libc++ and the MSVC standard library (which really needs a proper name so we don’t have to call it either “The MSVC implementation of the C++ standard library” or “The MSVC STL”).

Of course MSVC still won’t pass an optional<int> in a register because the MS ABI.

EDIT: This issue has been fixed in the GCC 8 release series.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)