From 9273af71636244395f2e5b311ccbc1b5ee9a667b Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 6 Mar 2024 03:05:18 +0000 Subject: [PATCH] Add constructor to Tuple on functors (RVO) --- include/utempl/tuple.hpp | 47 ++++++++++++++---------------------- include/utempl/type_list.hpp | 2 +- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/include/utempl/tuple.hpp b/include/utempl/tuple.hpp index 72ad36f..cac3b8e 100644 --- a/include/utempl/tuple.hpp +++ b/include/utempl/tuple.hpp @@ -18,10 +18,18 @@ template struct TupleHelper; template -struct TupleHelper, Ts...> : public TupleLeaf... { +struct TupleHelper, Ts...> : public impl::TupleLeaf... { inline constexpr bool operator==(const TupleHelper&) const = default; }; +template +struct Process { + using type = decltype(Overloaded( + [](TT&&) -> std::remove_cvref_t {}, + [](const char(&)[N]) -> const char* {} + )(std::declval())); +}; + } // namespace impl template @@ -51,17 +59,14 @@ inline constexpr auto Get(T&& arg) -> decltype(get(std::forward(arg))) { }; template -struct Tuple : public impl::TupleHelper, Ts...> { +struct Tuple : impl::TupleHelper, Ts...> { template inline constexpr Tuple(TTs&&... args) : impl::TupleHelper, Ts...>{{std::forward(args)}...} {}; - inline constexpr Tuple(const Ts&... args) : - impl::TupleHelper, Ts...>{{args}...} {}; - inline constexpr Tuple(Ts&... args) : - impl::TupleHelper, Ts...>{{args}...} {}; + template + inline constexpr Tuple(Fs&&... fs) requires (!std::invocable || ...) : + impl::TupleHelper, Ts...>{{fs()}...} {}; inline constexpr Tuple(Ts&&... args) : - impl::TupleHelper, Ts...>{{std::move(args)}...} {}; - inline constexpr Tuple(const Ts&&... args) : impl::TupleHelper, Ts...>{{args}...} {}; inline constexpr Tuple(const Tuple&) = default; inline constexpr Tuple(Tuple&&) = default; @@ -71,17 +76,14 @@ struct Tuple : public impl::TupleHelper, Ts...> { inline constexpr Tuple& operator=(Tuple&) = default; inline constexpr Tuple() : impl::TupleHelper, Ts...>() {}; - template - inline constexpr bool operator==(const Tuple& other) const - requires (TypeList{} == TypeList{}) { - return [&](std::index_sequence){ + inline constexpr bool operator==(const Tuple& other) const { + return [&](std::index_sequence){ return ((Get(*this) == Get(other)) && ...); - }(std::index_sequence_for()); - }; - + }(std::index_sequence_for()); + }; template inline constexpr auto operator+(const Tuple& other) const -> Tuple { - return [&](std::index_sequence, std::index_sequence) -> Tuple { + return [&](std::index_sequence, std::index_sequence) -> Tuple { return {Get(*this)..., Get(other)...}; }(std::index_sequence_for(), std::index_sequence_for()); }; @@ -107,19 +109,6 @@ struct Tuple<> { return true; }; }; - -namespace impl { - -template -struct Process { - using type = decltype(Overloaded( - [](TT&&) -> std::remove_cvref_t {}, - [](const char(&)[N]) -> const char* {} - )(std::declval())); -}; - -} // namespace impl - template Tuple(Ts&&...) -> Tuple::type...>; diff --git a/include/utempl/type_list.hpp b/include/utempl/type_list.hpp index 09cbb3d..4597a86 100644 --- a/include/utempl/type_list.hpp +++ b/include/utempl/type_list.hpp @@ -37,7 +37,7 @@ consteval auto operator+(const TypeList&, const TypeList&) -> Typ }; template -consteval auto Get(std::index_sequence, decltype(impl::Caster(Is))..., T, ...) -> T; +consteval auto Get(std::index_sequence, decltype(impl::Caster(Is))..., T&& arg, ...) -> T; template consteval auto Get(const TypeList&) -> decltype(Get(std::make_index_sequence(), std::declval()...)) requires (I < sizeof...(Ts));