File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,20 @@ using append_env_t = boost::mp11::mp_unique_if<
9393
9494template <_env::autowrap... Args>
9595using make_env_t = extend_env_t <env<>, Args...>;
96+
97+ template <envlike Env, typename Q> constexpr auto query (Q q) -> decltype(auto ) {
98+ return q (Env{});
99+ }
100+
101+ template <envlike Env, typename Q, typename Default>
102+ constexpr auto query (Q q, [[maybe_unused]] Default &&d) -> decltype(auto ) {
103+ if constexpr (requires { q (Env{}); }) {
104+ return q (Env{});
105+ } else {
106+ return std::forward<Default>(d);
107+ }
108+ }
109+
96110} // namespace v1
97111} // namespace stdx
98112
Original file line number Diff line number Diff line change @@ -18,10 +18,29 @@ namespace {
1818} custom;
1919} // namespace
2020
21- TEST_CASE (" lookup query with default" , " [env]" ) {
21+ TEST_CASE (" lookup query with internal default" , " [env]" ) {
2222 STATIC_REQUIRE (custom (stdx::env<>{}) == 42 );
2323}
2424
25+ TEST_CASE (" lookup query with default (free function)" , " [env]" ) {
26+ STATIC_REQUIRE (stdx::query<stdx::env<>>(custom) == 42 );
27+ }
28+
29+ namespace {
30+ [[maybe_unused]] constexpr inline struct custom_no_default_t {
31+ template <typename T>
32+ [[nodiscard]] CONSTEVAL auto operator ()(T &&t) const
33+ noexcept (noexcept (std::forward<T>(t).query(std::declval<custom_t >())))
34+ -> decltype(std::forward<T>(t).query(*this )) {
35+ return std::forward<T>(t).query (*this );
36+ }
37+ } custom_no_default;
38+ } // namespace
39+
40+ TEST_CASE (" lookup query with external default" , " [env]" ) {
41+ STATIC_REQUIRE (stdx::query<stdx::env<>>(custom_no_default, 42 ) == 42 );
42+ }
43+
2544TEST_CASE (" make an environment" , " [env]" ) {
2645 using E = stdx::make_env_t <custom, 17 >;
2746 STATIC_REQUIRE (custom (E{}) == 17 );
You can’t perform that action at this time.
0 commit comments