Since the std::variant
is not allowed to compare with one of its alternative types in the standard library, I am implementing the compare function using C++20 <=>
operator:
template <typename... Args, typename T>
constexpr auto operator<=>(const std::variant<Args...>& v, const T& t) {
return std::visit([&t](const auto& u) -> std::partial_ordering {
if constexpr (requires { u <=> t; }) return u <=> t;
else return std::partial_ordering::unordered;
}, v);
}
But when I testing the above function with my own defined std::variant
:
using Variant = std::variant<double, int, std::string_view>;
constexpr Variant v1{1.0};
constexpr Variant v2{1};
constexpr Variant v3{"hello"};
static_assert(v1 < 2);
// compile error
static_assert(v2 < 2);
static_assert(!(v3 > 2) && !(v3 < 2) && !std::is_eq(v3 <=> 2));
The second assertion couldn't compile, GCC says:
<source>:19:17: error: non-constant condition for static assertion
19 | static_assert(v2 < 2);
| ^~~~~~~~~
<source>:19:24: in 'constexpr' expansion of 'operator<=><double, int, std::basic_string_view<char, std::char_traits<char> >, int>(v2, 2)'
<source>:19:17: error: '<anonymous>' is not a constant expression
Why is v2 < 2
not a constant expression? or it's just a GCC bug? Weirder, when I change the second assertion to compare with double
, this can compile:
static_assert(v2 < 2.0);
Update:
Clang can pass three assertions, but MSVC can only pass the third assertion, it seems MSVC also has a bug.
visit
lambda returns different types for different invocations), although still a gcc bug for ICE-ing (ICE is always a bug). – Invoice