Skip to content

Commit 2949161

Browse files
authored
is()/as(): refactor of is() and as() for std::any and std::optional to new design (part 3 of n) (#1251)
* is()/as(): Refactor is() and as() for std::any * is()/as(): refactor is() and as() for std::optional * Fix tests
1 parent a18d22e commit 2949161

File tree

4 files changed

+72
-72
lines changed

4 files changed

+72
-72
lines changed

include/cpp2util.h

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,28 +2083,22 @@ auto as(X&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
20832083

20842084
// is Type
20852085
//
2086-
template<typename T, typename X>
2087-
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
2088-
constexpr auto is( X const& x ) -> bool
2089-
{ return x.type() == Typeid<T>(); }
2090-
2091-
template<typename T, typename X>
2092-
requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
2093-
constexpr auto is( X const& x ) -> bool
2094-
{ return !x.has_value(); }
2095-
2086+
template<typename T, std::same_as<std::any> X>
2087+
constexpr auto is( X const& x ) -> bool{
2088+
if (!x.has_value()) {
2089+
return std::is_same_v<T,empty>;
2090+
}
2091+
return x.type() == Typeid<T>();
2092+
}
20962093

20972094
// is Value
20982095
//
20992096
inline constexpr auto is( std::any const& x, auto&& value ) -> bool
21002097
{
21012098
// Predicate case
2102-
if constexpr (requires{ bool{ value(x) }; }) {
2099+
if constexpr (valid_predicate<decltype(value), decltype(x)>) {
21032100
return value(x);
21042101
}
2105-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
2106-
return false;
2107-
}
21082102

21092103
// Value case
21102104
else if constexpr (requires{ bool{ *std::any_cast<CPP2_TYPEOF(value)>(&x) == value }; }) {
@@ -2118,10 +2112,12 @@ inline constexpr auto is( std::any const& x, auto&& value ) -> bool
21182112

21192113
// as
21202114
//
2121-
template<typename T, typename X>
2122-
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
2123-
constexpr auto as( X const& x ) -> T
2124-
{ return std::any_cast<T>( x ); }
2115+
template<typename T, same_type_as<std::any> X>
2116+
constexpr auto as( X && x ) -> decltype(auto) {
2117+
constness_like_t<T, X>* ptr = std::any_cast<T>( &x );
2118+
if (!ptr) { Throw( std::bad_any_cast(), "'as' cast failed for 'std::any'"); }
2119+
return cpp2::forward_like<X>(*ptr);
2120+
}
21252121

21262122

21272123
//-------------------------------------------------------------------------------------------------------------
@@ -2130,29 +2126,26 @@ constexpr auto as( X const& x ) -> T
21302126

21312127
// is Type
21322128
//
2133-
template<typename T, typename X>
2134-
requires std::is_same_v<X,std::optional<T>>
2135-
constexpr auto is( X const& x ) -> bool
2136-
{ return x.has_value(); }
2137-
2138-
template<typename T, typename U>
2139-
requires std::is_same_v<T,empty>
2140-
constexpr auto is( std::optional<U> const& x ) -> bool
2141-
{ return !x.has_value(); }
2142-
2129+
template<typename T, specialization_of_template<std::optional> X>
2130+
constexpr auto is( X const& x ) -> bool {
2131+
if (!x.has_value()) {
2132+
return std::same_as<T, empty>;
2133+
}
2134+
if constexpr (requires { static_cast<const T&>(*x);}) {
2135+
return true;
2136+
}
2137+
return false;
2138+
}
21432139

21442140
// is Value
21452141
//
21462142
template<typename T>
21472143
constexpr auto is( std::optional<T> const& x, auto&& value ) -> bool
21482144
{
21492145
// Predicate case
2150-
if constexpr (requires{ bool{ value(x) }; }) {
2146+
if constexpr (valid_predicate<decltype(value), decltype(x)>) {
21512147
return value(x);
21522148
}
2153-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
2154-
return false;
2155-
}
21562149

21572150
// Value case
21582151
else if constexpr (requires{ bool{ x.value() == value }; }) {
@@ -2164,10 +2157,17 @@ constexpr auto is( std::optional<T> const& x, auto&& value ) -> bool
21642157

21652158
// as
21662159
//
2167-
template<typename T, typename X>
2168-
requires std::is_same_v<X,std::optional<T>>
2169-
constexpr auto as( X const& x ) -> decltype(auto)
2170-
{ return x.value(); }
2160+
template<typename T, specialization_of_template<std::optional> X>
2161+
constexpr auto as( X&& x ) -> decltype(auto) {
2162+
constness_like_t<T, X>* ptr = nullptr;
2163+
if constexpr (requires { static_cast<constness_like_t<T, X>&>(*x); }) {
2164+
if (x.has_value()) {
2165+
ptr = &static_cast<constness_like_t<T, X>&>(*x);
2166+
}
2167+
}
2168+
if (!ptr) { Throw( std::bad_optional_access(), "'as' cast failed for 'std::optional'"); }
2169+
return cpp2::forward_like<X>(*ptr);
2170+
}
21712171

21722172

21732173
} // impl
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
In file included from pure2-default-arguments.cpp:7:
22
../../../include/cpp2util.h:2086:28: error: local variable ‘obj’ may not appear in this context
3-
2086 | template<typename T, typename X>
3+
2086 | template<typename T, std::same_as<std::any> X>
44
| ^~~
55
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
66
2047 | return false;
77
| ^
88
../../../include/cpp2util.h:2086:15: note: in expansion of macro ‘CPP2_FORWARD’
9-
2086 | template<typename T, typename X>
9+
2086 | template<typename T, std::same_as<std::any> X>
1010
| ^~~~~~~~~~~~
1111
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
1212
2107 | }
1313
| ^
1414
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
15-
2137 |
15+
2137 | return false;
1616
| ^
1717
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
1818
../../../include/cpp2util.h:2086:92: error: local variable ‘params’ may not appear in this context
19-
2086 | template<typename T, typename X>
19+
2086 | template<typename T, std::same_as<std::any> X>
2020
| ^
2121
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
2222
2047 | return false;
2323
| ^
2424
../../../include/cpp2util.h:2086:79: note: in expansion of macro ‘CPP2_FORWARD’
25-
2086 | template<typename T, typename X>
25+
2086 | template<typename T, std::same_as<std::any> X>
2626
| ^
2727
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
2828
2107 | }
2929
| ^
3030
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
31-
2137 |
31+
2137 | return false;
3232
| ^
3333
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
3434
../../../include/cpp2util.h:2087:74: error: local variable ‘obj’ may not appear in this context
35-
2087 | requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
36-
| ^~~
35+
2087 | constexpr auto is( X const& x ) -> bool{
36+
| ^
3737
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
3838
2047 | return false;
3939
| ^
4040
../../../include/cpp2util.h:2087:61: note: in expansion of macro ‘CPP2_FORWARD’
41-
2087 | requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
42-
| ^~~~~~~~~~~~
41+
2087 | constexpr auto is( X const& x ) -> bool{
42+
| ^
4343
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
4444
2107 | }
4545
| ^
4646
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
47-
2137 |
47+
2137 | return false;
4848
| ^
4949
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
5050
../../../include/cpp2util.h:2087:93: error: local variable ‘params’ may not appear in this context
51-
2087 | requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
52-
| ^~~~~~
51+
2087 | constexpr auto is( X const& x ) -> bool{
52+
| ^
5353
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
5454
2047 | return false;
5555
| ^
5656
../../../include/cpp2util.h:2087:80: note: in expansion of macro ‘CPP2_FORWARD’
57-
2087 | requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
58-
| ^~~~~~~~~~~~
57+
2087 | constexpr auto is( X const& x ) -> bool{
58+
| ^
5959
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
6060
2107 | }
6161
| ^
6262
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
63-
2137 |
63+
2137 | return false;
6464
| ^
6565
pure2-default-arguments.cpp2:6:22: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
6666
pure2-default-arguments.cpp2:6:61: error: ‘std::source_location’ has not been declared
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
In file included from mixed-bugfix-for-ufcs-non-local.cpp:6:
22
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
3-
2100 | {
3+
2100 | return value(x);
44
| ^
55
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
6-
2137 |
6+
2137 | return false;
77
| ^
88
mixed-bugfix-for-ufcs-non-local.cpp2:13:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
99
mixed-bugfix-for-ufcs-non-local.cpp2:13:36: error: template argument 1 is invalid
1010
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
11-
2100 | {
11+
2100 | return value(x);
1212
| ^
1313
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
14-
2137 |
14+
2137 | return false;
1515
| ^
1616
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
1717
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid
1818
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
19-
2100 | {
19+
2100 | return value(x);
2020
| ^
2121
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
22-
2137 |
22+
2137 | return false;
2323
| ^
2424
mixed-bugfix-for-ufcs-non-local.cpp2:31:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
2525
mixed-bugfix-for-ufcs-non-local.cpp2:31:36: error: template argument 1 is invalid
2626
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
27-
2100 | {
27+
2100 | return value(x);
2828
| ^
2929
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
30-
2137 |
30+
2137 | return false;
3131
| ^
3232
mixed-bugfix-for-ufcs-non-local.cpp2:33:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
3333
mixed-bugfix-for-ufcs-non-local.cpp2:33:36: error: template argument 1 is invalid
3434
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
35-
2100 | {
35+
2100 | return value(x);
3636
| ^
3737
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
38-
2137 |
38+
2137 | return false;
3939
| ^
4040
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
4141
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
In file included from mixed-bugfix-for-ufcs-non-local.cpp:6:
22
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
3-
2100 | {
3+
2100 | return value(x);
44
| ^
55
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
6-
2137 |
6+
2137 | return false;
77
| ^
88
mixed-bugfix-for-ufcs-non-local.cpp2:13:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
99
mixed-bugfix-for-ufcs-non-local.cpp2:13:36: error: template argument 1 is invalid
1010
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
11-
2100 | {
11+
2100 | return value(x);
1212
| ^
1313
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
14-
2137 |
14+
2137 | return false;
1515
| ^
1616
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
1717
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid
1818
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
19-
2100 | {
19+
2100 | return value(x);
2020
| ^
2121
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
22-
2137 |
22+
2137 | return false;
2323
| ^
2424
mixed-bugfix-for-ufcs-non-local.cpp2:31:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
2525
mixed-bugfix-for-ufcs-non-local.cpp2:31:36: error: template argument 1 is invalid
2626
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
27-
2100 | {
27+
2100 | return value(x);
2828
| ^
2929
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
30-
2137 |
30+
2137 | return false;
3131
| ^
3232
mixed-bugfix-for-ufcs-non-local.cpp2:33:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
3333
mixed-bugfix-for-ufcs-non-local.cpp2:33:36: error: template argument 1 is invalid
3434
../../../include/cpp2util.h:2100:1: error: lambda-expression in template parameter type
35-
2100 | {
35+
2100 | return value(x);
3636
| ^
3737
../../../include/cpp2util.h:2137:59: note: in expansion of macro ‘CPP2_UFCS_’
38-
2137 |
38+
2137 | return false;
3939
| ^
4040
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: note: in expansion of macro ‘CPP2_UFCS_NONLOCAL’
4141
mixed-bugfix-for-ufcs-non-local.cpp2:21:36: error: template argument 1 is invalid

0 commit comments

Comments
 (0)