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

后端 未结 4 629
死守一世寂寞
死守一世寂寞 2020-12-08 09:07

Consider these two approaches that can represent an \"optional int\":

using std_optional_int = std::optional;
using my_optional_int =         


        
4条回答
  •  余生分开走
    2020-12-08 09:25

    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>);
    

    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 in a register because the MS ABI.

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

提交回复
热议问题