https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96497

            Bug ID: 96497
           Summary: Compare std::variant with int using C++20 <=> is not a
                    constant expression
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

#include <compare>
#include <variant>

using variant = std::variant<int, double>;

constexpr auto operator<=>(const variant& v, const auto& u) {
  return std::visit([&u](const auto& t) -> std::partial_ordering { return t <=>
u; }, v);
}

constexpr variant v{1};
static_assert(v < 2);

This fails on gcc 11.0 with:
<source>: In function 'int main()':
<source>:12:19: error: non-constant condition for static assertion
   12 |   static_assert(v < 2);
      |                 ~~^~~
<source>:12:19: error: '<anonymous>' is not a constant expression

but if I initialize the v with double, it can compile:

constexpr variant v{1.0};
static_assert(v < 2);

godbolt link:
https://godbolt.org/z/eE9c97

Reply via email to