Skip to content
Merged
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
14 changes: 14 additions & 0 deletions include/stdx/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ using append_env_t = boost::mp11::mp_unique_if<

template <_env::autowrap... Args>
using make_env_t = extend_env_t<env<>, Args...>;

template <envlike Env, typename Q> constexpr auto query(Q q) -> decltype(auto) {
return q(Env{});
}

template <envlike Env, typename Q, typename Default>
constexpr auto query(Q q, [[maybe_unused]] Default &&d) -> decltype(auto) {
if constexpr (requires { q(Env{}); }) {
return q(Env{});
} else {
return std::forward<Default>(d);
}
}

} // namespace v1
} // namespace stdx

Expand Down
21 changes: 20 additions & 1 deletion test/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,29 @@ namespace {
} custom;
} // namespace

TEST_CASE("lookup query with default", "[env]") {
TEST_CASE("lookup query with internal default", "[env]") {
STATIC_REQUIRE(custom(stdx::env<>{}) == 42);
}

TEST_CASE("lookup query with default (free function)", "[env]") {
STATIC_REQUIRE(stdx::query<stdx::env<>>(custom) == 42);
}

namespace {
[[maybe_unused]] constexpr inline struct custom_no_default_t {
template <typename T>
[[nodiscard]] CONSTEVAL auto operator()(T &&t) const
noexcept(noexcept(std::forward<T>(t).query(std::declval<custom_t>())))
-> decltype(std::forward<T>(t).query(*this)) {
return std::forward<T>(t).query(*this);
}
} custom_no_default;
} // namespace

TEST_CASE("lookup query with external default", "[env]") {
STATIC_REQUIRE(stdx::query<stdx::env<>>(custom_no_default, 42) == 42);
}

TEST_CASE("make an environment", "[env]") {
using E = stdx::make_env_t<custom, 17>;
STATIC_REQUIRE(custom(E{}) == 17);
Expand Down