Skip to content

Commit cf1349d

Browse files
committed
Merge 'main' into 'range-second-ctor-ctad'
2 parents 1e56dca + 2949161 commit cf1349d

File tree

4 files changed

+86
-86
lines changed

4 files changed

+86
-86
lines changed

include/cpp2util.h

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

20892089
// is Type
20902090
//
2091-
template<typename T, typename X>
2092-
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
2093-
constexpr auto is( X const& x ) -> bool
2094-
{ return x.type() == Typeid<T>(); }
2095-
2096-
template<typename T, typename X>
2097-
requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
2098-
constexpr auto is( X const& x ) -> bool
2099-
{ return !x.has_value(); }
2100-
2091+
template<typename T, std::same_as<std::any> X>
2092+
constexpr auto is( X const& x ) -> bool{
2093+
if (!x.has_value()) {
2094+
return std::is_same_v<T,empty>;
2095+
}
2096+
return x.type() == Typeid<T>();
2097+
}
21012098

21022099
// is Value
21032100
//
21042101
inline constexpr auto is( std::any const& x, auto&& value ) -> bool
21052102
{
21062103
// Predicate case
2107-
if constexpr (requires{ bool{ value(x) }; }) {
2104+
if constexpr (valid_predicate<decltype(value), decltype(x)>) {
21082105
return value(x);
21092106
}
2110-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
2111-
return false;
2112-
}
21132107

21142108
// Value case
21152109
else if constexpr (requires{ bool{ *std::any_cast<CPP2_TYPEOF(value)>(&x) == value }; }) {
@@ -2123,10 +2117,12 @@ inline constexpr auto is( std::any const& x, auto&& value ) -> bool
21232117

21242118
// as
21252119
//
2126-
template<typename T, typename X>
2127-
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
2128-
constexpr auto as( X const& x ) -> T
2129-
{ return std::any_cast<T>( x ); }
2120+
template<typename T, same_type_as<std::any> X>
2121+
constexpr auto as( X && x ) -> decltype(auto) {
2122+
constness_like_t<T, X>* ptr = std::any_cast<T>( &x );
2123+
if (!ptr) { Throw( std::bad_any_cast(), "'as' cast failed for 'std::any'"); }
2124+
return cpp2::forward_like<X>(*ptr);
2125+
}
21302126

21312127

21322128
//-------------------------------------------------------------------------------------------------------------
@@ -2135,29 +2131,26 @@ constexpr auto as( X const& x ) -> T
21352131

21362132
// is Type
21372133
//
2138-
template<typename T, typename X>
2139-
requires std::is_same_v<X,std::optional<T>>
2140-
constexpr auto is( X const& x ) -> bool
2141-
{ return x.has_value(); }
2142-
2143-
template<typename T, typename U>
2144-
requires std::is_same_v<T,empty>
2145-
constexpr auto is( std::optional<U> const& x ) -> bool
2146-
{ return !x.has_value(); }
2147-
2134+
template<typename T, specialization_of_template<std::optional> X>
2135+
constexpr auto is( X const& x ) -> bool {
2136+
if (!x.has_value()) {
2137+
return std::same_as<T, empty>;
2138+
}
2139+
if constexpr (requires { static_cast<const T&>(*x);}) {
2140+
return true;
2141+
}
2142+
return false;
2143+
}
21482144

21492145
// is Value
21502146
//
21512147
template<typename T>
21522148
constexpr auto is( std::optional<T> const& x, auto&& value ) -> bool
21532149
{
21542150
// Predicate case
2155-
if constexpr (requires{ bool{ value(x) }; }) {
2151+
if constexpr (valid_predicate<decltype(value), decltype(x)>) {
21562152
return value(x);
21572153
}
2158-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
2159-
return false;
2160-
}
21612154

21622155
// Value case
21632156
else if constexpr (requires{ bool{ x.value() == value }; }) {
@@ -2169,10 +2162,17 @@ constexpr auto is( std::optional<T> const& x, auto&& value ) -> bool
21692162

21702163
// as
21712164
//
2172-
template<typename T, typename X>
2173-
requires std::is_same_v<X,std::optional<T>>
2174-
constexpr auto as( X const& x ) -> decltype(auto)
2175-
{ return x.value(); }
2165+
template<typename T, specialization_of_template<std::optional> X>
2166+
constexpr auto as( X&& x ) -> decltype(auto) {
2167+
constness_like_t<T, X>* ptr = nullptr;
2168+
if constexpr (requires { static_cast<constness_like_t<T, X>&>(*x); }) {
2169+
if (x.has_value()) {
2170+
ptr = &static_cast<constness_like_t<T, X>&>(*x);
2171+
}
2172+
}
2173+
if (!ptr) { Throw( std::bad_optional_access(), "'as' cast failed for 'std::optional'"); }
2174+
return cpp2::forward_like<X>(*ptr);
2175+
}
21762176

21772177

21782178
} // impl
Lines changed: 30 additions & 30 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 | // std::any is and as
4-
| ^
3+
2086 | template<typename T, std::same_as<std::any> X>
4+
| ^~~
55
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
6-
2047 | return std::get_if<std::monostate>(&x) != nullptr;
7-
| ^~~~~~~~~~~
6+
2047 | return false;
7+
| ^
88
../../../include/cpp2util.h:2086:15: note: in expansion of macro ‘CPP2_FORWARD’
9-
2086 | // std::any is and as
10-
| ^~~~~~~~
9+
2086 | template<typename T, std::same_as<std::any> X>
10+
| ^~~~~~~~~~~~
1111
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
12-
2107 | if constexpr (requires{ bool{ value(x) }; }) {
13-
| ^~~~~~~~~~~~~~~~~~~~~~~~
12+
2107 | }
13+
| ^
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 | // std::any is and as
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’
22-
2047 | return std::get_if<std::monostate>(&x) != nullptr;
23-
| ^~~~~~~~~~~
22+
2047 | return false;
23+
| ^
2424
../../../include/cpp2util.h:2086:79: note: in expansion of macro ‘CPP2_FORWARD’
25-
2086 | // std::any is and as
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’
28-
2107 | if constexpr (requires{ bool{ value(x) }; }) {
29-
| ^~~~~~~~~~~~~~~~~~~~~~~~
28+
2107 | }
29+
| ^
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 | //
35+
2087 | constexpr auto is( X const& x ) -> bool{
3636
| ^
3737
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
38-
2047 | return std::get_if<std::monostate>(&x) != nullptr;
39-
| ^~~~~~~~~~~
38+
2047 | return false;
39+
| ^
4040
../../../include/cpp2util.h:2087:61: note: in expansion of macro ‘CPP2_FORWARD’
41-
2087 | //
41+
2087 | constexpr auto is( X const& x ) -> bool{
4242
| ^
4343
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
44-
2107 | if constexpr (requires{ bool{ value(x) }; }) {
45-
| ^~~~~~~~~~~~~~~~~~~~~~~~
44+
2107 | }
45+
| ^
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 | //
51+
2087 | constexpr auto is( X const& x ) -> bool{
5252
| ^
5353
../../../include/cpp2util.h:2047:34: note: in definition of macro ‘CPP2_UFCS_IDENTITY’
54-
2047 | return std::get_if<std::monostate>(&x) != nullptr;
55-
| ^~~~~~~~~~~
54+
2047 | return false;
55+
| ^
5656
../../../include/cpp2util.h:2087:80: note: in expansion of macro ‘CPP2_FORWARD’
57-
2087 | //
57+
2087 | constexpr auto is( X const& x ) -> bool{
5858
| ^
5959
../../../include/cpp2util.h:2107:22: note: in expansion of macro ‘CPP2_UFCS_CONSTRAINT_ARG’
60-
2107 | if constexpr (requires{ bool{ value(x) }; }) {
61-
| ^~~~~~~~~~~~~~~~~~~~~~~~
60+
2107 | }
61+
| ^
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)