11#include <catch2/catch.hpp>
22 operator int()
const {
return x; }
28template<
bool Const, Ref ref = lvalue>
36 std::conditional_t<ref ==
rvalue,
37 std::add_rvalue_reference_t<MaybeConst<ValueType>>,
38 std::add_lvalue_reference_t<MaybeConst<ValueType>>
45 if constexpr (ref ==
rvalue)
57 -> std::enable_if_t<!C, ResultType> {
return value<1>(); }
69 return [u = std::make_unique<X>()](double) ->
ResultType
70 {
return value<4>(); };
107template<
typename Visitor,
typename Arg>
108static void testCase(
const Visitor &visitor,
int result, Arg &arg)
112 const std::remove_reference_t<
decltype(arg)> carg{ arg };
114 REQUIRE(result == visitor(carg));
115 REQUIRE(result ==
Visit(visitor, cv));
116 if constexpr (ref !=
lvalue) {
117 REQUIRE(result == visitor(std::move(carg)));
118 REQUIRE(result ==
Visit(visitor, move(cv)));
124 REQUIRE(result == visitor(arg));
125 REQUIRE(result ==
Visit(visitor, v));
126 if constexpr (ref !=
lvalue) {
127 REQUIRE(result == visitor(std::move(arg)));
128 REQUIRE(result ==
Visit(visitor, move(v)));
141 &Y::template memberfunction<Const>,
157 if constexpr (ref ==
lvalue)
167 REQUIRE(6 == std::move(visitor)(ld));
168 using ConstVisitorType =
decltype(visitor);
169 using VisitorType = std::remove_const_t<ConstVisitorType>;
170 auto &mutVisitor =
const_cast<VisitorType&
>(visitor);
171 REQUIRE(7 == mutVisitor(ld));
172 REQUIRE(8 == std::move(mutVisitor)(ld));
227 static_assert(std::is_same_v<
int&,
decltype(
Visit(vis, var))>);
228 static_assert(std::is_same_v<
int&,
decltype(
Visit(cvis, var))>);
229 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(vis), var))>);
230 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(cvis), var))>);
232 static_assert(std::is_same_v<
int&,
decltype(
Visit(vis, cvar))>);
233 static_assert(std::is_same_v<
int&,
decltype(
Visit(cvis, cvar))>);
234 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(vis), cvar))>);
235 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(cvis), cvar))>);
237 static_assert(std::is_same_v<
int&,
decltype(
Visit(vis, std::move(var)))>);
238 static_assert(std::is_same_v<
int&,
decltype(
Visit(cvis, std::move(var)))>);
239 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(vis), std::move(var)))>);
240 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(cvis), std::move(var)))>);
242 static_assert(std::is_same_v<
int&,
decltype(
Visit(vis, std::move(cvar)))>);
243 static_assert(std::is_same_v<
int&,
decltype(
Visit(cvis, std::move(cvar)))>);
244 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(vis), std::move(cvar)))>);
245 static_assert(std::is_same_v<
int&,
decltype(
Visit(std::move(cvis), std::move(cvar)))>);
Functions and classes that generate callable objects.
emulates std::visit for one visitor
static void compileTest()
TEST_CASE("Variant visitors returning T &")
OverloadSet(Is &&... invocables) -> OverloadSet< Is &&... >
int & operator()(std::monostate) const
ResultType operator()(long double) const &
CopyOnly(CopyOnly &&)=delete
CopyOnly(const CopyOnly &)=default
Structure can specialize with only a non-const member function.
auto memberfunction() const -> std::enable_if_t< C, ResultType >
auto memberfunction() -> std::enable_if_t<!C, ResultType >
Structure always with a const member function.
ResultType constmemberfunction() const
Parameterize for the type of visitor return.
static ResultType value()
static ResultType nakedFunction(float)
std::conditional_t< ref==noref, MaybeConst< ValueType >, std::conditional_t< ref==rvalue, std::add_rvalue_reference_t< MaybeConst< ValueType > >, std::add_lvalue_reference_t< MaybeConst< ValueType > > > > ResultType
std::conditional_t< Const, const T, T > MaybeConst
static void testCase(const Visitor &visitor, int result, Arg &arg)
std::conditional_t< ref==lvalue, std::variant< MaybeConst< X >, MaybeConst< Y >, MaybeConst< Z >, MaybeConst< float >, MaybeConst< double >, MaybeConst< long double > >, std::variant< MaybeConst< Y >, MaybeConst< Z >, MaybeConst< float >, MaybeConst< double >, MaybeConst< long double > > > VariantType