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
9 changes: 9 additions & 0 deletions docs/functional.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ v.emplace(0, stdx::with_result_of{make_S}); // this constructs S in-place thanks

`with_result_of` can help to achieve in-place construction, effectively by deferring
evaluation of function arguments.

=== `unary_plus`

`unary_plus` is a function object like
https://en.cppreference.com/w/cpp/utility/functional/negate.html[`std::negate`],
except it calls (unary) `operator+` instead of `operator-`.

It also has a specialization for `void` which is _transparent_ like that of
https://en.cppreference.com/w/cpp/utility/functional/negate_void.html[`std::negate`].
17 changes: 17 additions & 0 deletions include/stdx/functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,22 @@ template <auto F, typename... Args> constexpr auto bind_back(Args &&...args) {
}

#endif

template <typename T = void> struct unary_plus {
constexpr auto operator()(T const &arg) const -> decltype(+arg) {
return +arg;
}
};

template <> struct unary_plus<void> {
using is_transparent = int;

template <typename T>
constexpr auto operator()(T &&arg) const
-> decltype(+std::forward<T>(arg)) {
return +std::forward<T>(arg);
}
};

} // namespace v1
} // namespace stdx
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_tests(
default_panic
for_each_n_args
function_traits
functional
intrusive_forward_list
intrusive_list
intrusive_list_properties
Expand Down
30 changes: 30 additions & 0 deletions test/functional.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdx/functional.hpp>

#include <catch2/catch_test_macros.hpp>

#include <type_traits>

namespace {
template <typename T, typename = void>
constexpr auto detect_is_transparent = false;
template <typename T>
constexpr auto
detect_is_transparent<T, std::void_t<typename T::is_transparent>> = true;

struct S {};
constexpr auto operator+(S) { return 17; }
} // namespace

TEST_CASE("unary_plus", "[functional]") {
STATIC_REQUIRE(stdx::unary_plus<int>{}(17) == 17);
STATIC_REQUIRE(stdx::unary_plus<>{}(17) == 17);
}

TEST_CASE("unary_plus transparency", "[functional]") {
STATIC_REQUIRE(not detect_is_transparent<stdx::unary_plus<int>>);
STATIC_REQUIRE(detect_is_transparent<stdx::unary_plus<>>);
}

TEST_CASE("unary_plus calls operator+", "[functional]") {
STATIC_REQUIRE(stdx::unary_plus<>{}(S{}) == 17);
}
Loading