Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

env:
UBSAN_OPTIONS: print_stacktrace=1
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true

jobs:
posix:
Expand Down
18 changes: 18 additions & 0 deletions doc/variant2/reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() == std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: :: `v.index() == w.index() && get<I>(v) == get<I>(w)`, where `I`
is `v.index()`.

Expand All @@ -855,6 +858,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() != std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: :: `!(v == w)`.

```
Expand All @@ -864,6 +870,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() < std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: :: `v.index() < w.index() || (v.index() == w.index() && get<I>(v) < get<I>(w))`,
where `I` is `v.index()`.

Expand All @@ -874,6 +883,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() < std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: :: `w < v`.

```
Expand All @@ -883,6 +895,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() \<= std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: :: `v.index() < w.index() || (v.index() == w.index() && get<I>(v) \<= get<I>(w))`,
where `I` is `v.index()`.

Expand All @@ -893,6 +908,9 @@ template<class... T>
[none]
* {blank}
+
Constraints: :: `std::declval<const Ti&>() \<= std::declval<const Ti&>()` is a valid expression
convertible to `bool` for all `Ti` in `T...`.
+
Returns: ::
`w \<= v`.

Expand Down
79 changes: 73 additions & 6 deletions include/boost/variant2/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,45 @@ template<class... T> class variant: private detail::variant_ma_base<T...>
namespace detail
{

namespace relops_constraints
{

#if !defined(BOOST_NO_SFINAE_EXPR) && \
!BOOST_WORKAROUND(BOOST_MSVC, < 1900) && !BOOST_WORKAROUND(BOOST_GCC, < 40900)

template<class...> struct make_void { typedef void type; };

#define BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( name, op ) \
template<class T, class = void> struct name: std::false_type {}; \
\
template<class T> struct name<T, typename make_void< \
decltype( std::declval<const T&>() op std::declval<const T&>() )>::type>: \
std::is_convertible<decltype( std::declval<const T&>() op std::declval<const T&>() ), bool> \
{ \
};

#else

// non-expression-SFINAE fallback does not work with deleted relops:
// variant's relop instantiation fails instead, which is good enough

struct not_comparable {};
template<class U> not_comparable operator==( U const &, U const & );
template<class U> not_comparable operator!=( U const &, U const & );
template<class U> not_comparable operator< ( U const &, U const & );
template<class U> not_comparable operator<=( U const &, U const & );

#define BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( name, op ) \
template<class T> struct name: std::is_convertible<decltype( std::declval<const T&>() op std::declval<const T&>() ), bool> \
{ \
};

#endif

BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( has_eq, == )

} // namespace relops_constraints

template<class... T> struct eq_L
{
variant<T...> const & v;
Expand All @@ -2116,14 +2155,22 @@ template<class... T> struct eq_L

} // namespace detail

template<class... T> constexpr bool operator==( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_eq<T>...>::value>::type>
constexpr bool operator==( variant<T...> const & v, variant<T...> const & w )
{
return v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::eq_L<T...>{ v, w } );
}

namespace detail
{

namespace relops_constraints
{

BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( has_ne, != )

} // namespace relops_constraints

template<class... T> struct ne_L
{
variant<T...> const & v;
Expand All @@ -2137,14 +2184,22 @@ template<class... T> struct ne_L

} // namespace detail

template<class... T> constexpr bool operator!=( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_ne<T>...>::value>::type>
constexpr bool operator!=( variant<T...> const & v, variant<T...> const & w )
{
return v.index() != w.index() || mp11::mp_with_index<sizeof...(T)>( v.index(), detail::ne_L<T...>{ v, w } );
}

namespace detail
{

namespace relops_constraints
{

BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( has_lt, < )

} // namespace relops_constraints

template<class... T> struct lt_L
{
variant<T...> const & v;
Expand All @@ -2158,19 +2213,29 @@ template<class... T> struct lt_L

} // namespace detail

template<class... T> constexpr bool operator<( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_lt<T>...>::value>::type>
constexpr bool operator<( variant<T...> const & v, variant<T...> const & w )
{
return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::lt_L<T...>{ v, w } ) );
}

template<class... T> constexpr bool operator>( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_lt<T>...>::value>::type>
constexpr bool operator>( variant<T...> const & v, variant<T...> const & w )
{
return w < v;
}

namespace detail
{

namespace relops_constraints
{

BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT( has_le, <= )
#undef BOOST_VARIANT2_DEFINE_RELOP_CONSTRAINT

} // namespace relops_constraints

template<class... T> struct le_L
{
variant<T...> const & v;
Expand All @@ -2184,12 +2249,14 @@ template<class... T> struct le_L

} // namespace detail

template<class... T> constexpr bool operator<=( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_le<T>...>::value>::type>
constexpr bool operator<=( variant<T...> const & v, variant<T...> const & w )
{
return v.index() < w.index() || ( v.index() == w.index() && mp11::mp_with_index<sizeof...(T)>( v.index(), detail::le_L<T...>{ v, w } ) );
}

template<class... T> constexpr bool operator>=( variant<T...> const & v, variant<T...> const & w )
template<class... T, class E = typename std::enable_if<mp11::mp_all<detail::relops_constraints::has_le<T>...>::value>::type>
constexpr bool operator>=( variant<T...> const & v, variant<T...> const & w )
{
return w <= v;
}
Expand Down
2 changes: 2 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ run variant_issue_55.cpp
# clang-cl 32 bit fails with an assertion in mp_with_index, likely due to a codegen bug
"<toolset>clang-win,<address-model>32:<build>no"
;

run variant_constrained_relops.cpp ;
Loading
Loading