https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91788
Bug ID: 91788 Summary: std::variant index +1-1 Product: gcc Version: 10.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Target Milestone: --- (this is a detail, it probably has a negligible impact) constexpr size_t index() const noexcept { return size_t(typename _Base::__index_type(this->_M_index + 1)) - 1; } IIUC, the whole +1-1 is here so that for a valueless variant, index_type(-1) becomes size_t(-1). I think there are cases where we could do better. For instance, for a never valueless type, we could just return _M_index. If there are fewer than 128 alternatives, we could use a sign extension: "return (signed char)_M_index;". Maybe some well-placed __builtin_unreachable to specify the range of _M_index would work as well.