If a std::variant can never get into valueless state then we don't need to do a runtime check for a valid alternative.
PR libstdc++/87431 * include/std/variant (_Variant_storage<true, _Types...>::_M_valid): Avoid runtime test when all alternatives are scalars and so cannot throw during initialization. Tested x86_64-linux, committed to trunk.
commit 0140d5baa899fc4b6ee47636d607d8d22fc0ab5a Author: Jonathan Wakely <jwak...@redhat.com> Date: Tue Sep 25 15:03:46 2018 +0100 PR libstdc++/87431 optimise valueless_by_exception() If a std::variant can never get into valueless state then we don't need to do a runtime check for a valid alternative. PR libstdc++/87431 * include/std/variant (_Variant_storage<true, _Types...>::_M_valid): Avoid runtime test when all alternatives are scalars and so cannot throw during initialization. diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 5a77e9e2d84..9289eef28cf 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -439,6 +439,8 @@ namespace __variant constexpr bool _M_valid() const noexcept { + if constexpr ((is_scalar_v<_Types> && ...)) + return true; return this->_M_index != __index_type(variant_npos); }