From 1f910a8a87b8e243ef92255295c3b7e02f2bae54 Mon Sep 17 00:00:00 2001 From: Kris Jusiak Date: Sun, 18 Jun 2023 13:01:53 -0500 Subject: [PATCH] [335] - Did you know that you can simplify `boost.mp11` API with DSL? --- README.md | 11 +++++----- tips/334.md | 15 +++++++------ tips/335.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 tips/335.md diff --git a/README.md b/README.md index 7382ef6..e15ee63 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,13 @@ ### Tips -* [[334]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/334.md) - Did you know that C++23 added std::invoke_r? -* [[333]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/333.md) - Did you know that C++20 added std::span? +* [[335]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/335.md) - Did you know that you can simplify `boost.mp11` API with DSL? +* [[334]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/334.md) - Did you know that C++23 added `std::invoke_r`? +* [[333]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/333.md) - Did you know that C++20 added `std::span`? * [[332]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/332.md) - Did you know that in C++ you can generate jump tables at compile-time? -* [[331]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/331.md) - Did you about C++17 std::index_sequence, std::make_index_sequence? -* [[330]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/330.md) - Did you know that C++17 added std::pmr::polymorphic_allocator? -* [[329]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/329.md) - Did you know about C++ allows to pass Pointer To Member Function via template parameter? +* [[331]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/331.md) - Did you about C++17 `std::index_sequence, std::make_index_sequence`? +* [[330]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/330.md) - Did you know that C++17 added `std::pmr::polymorphic_allocator`? +* [[329]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/329.md) - Did you know that C++ allows to pass Pointer To Member Function via template parameter? * [[328]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/328.md) - Did you know that C++23 extended floating-point types? * [[327]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/327.md) - Did you know that C++17 added `std::forward_as_tuple` and `std::make_from_tuple` and what’s the difference between them? * [[326]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/326.md) - Did you know that C++23 deprecated std::aligned_storage and std::aligned_union? diff --git a/tips/334.md b/tips/334.md index f28321c..d80d986 100644 --- a/tips/334.md +++ b/tips/334.md @@ -42,11 +42,6 @@ static_assert(typeid(unsigned long long) == typeid(call(1ll, 2ull, 3l)));

Solutions

```cpp -#include -#include - -constexpr auto sum(auto... ts) { return (ts + ...); } - [[nodiscard]] constexpr auto call(auto... ts) { using Ret = std::common_type_t; return std::invoke_r(sum, ts...); @@ -55,7 +50,13 @@ constexpr auto sum(auto... ts) { return (ts + ...); } > https://godbolt.org/z/1baMcc331 -

-
Solutions

+```cpp +[[nodiscard]] constexpr auto call(auto... ts){ + using T = typename std::common_type_t; + return std::invoke_r(&sum, ts...); +} +``` + > https://godbolt.org/z/xcn9qnrcY +

diff --git a/tips/335.md b/tips/335.md new file mode 100644 index 0000000..85b40d7 --- /dev/null +++ b/tips/335.md @@ -0,0 +1,61 @@ +
Info

+ +* Did you know that you can simplify `boost.mp11` API with DSL*? + + * https://github.com/boostorg/mp11 + * DSL - Domain Specific Language + +

Example

+ +```cpp +template +[[nodiscard]] constexpr auto operator==(const T1&, const T2&) { return std::is_same_v; } + +template +constexpr auto list = mp11::mp_list{}; + +static_assert(list<> == list<>); +static_assert(list == list); +static_assert(list == list); +``` + +> https://godbolt.org/z/fTMsK8Moh + +

Puzzle

+ +* Can you implement DSL for boost.mp11 + +```cpp +struct event {}; +class foo1 : event {}; +class foo2 : event { int i; }; +class bar1 {}; +class bar2 { int i; }; + +template +constexpr auto unique_event_ptrs = + list + | unique + | filter> + | transform + ; + +static_assert(unique_event_ptrs<> == list<>); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list<>); +static_assert(unique_event_ptrs == list<>); +static_assert(unique_event_ptrs == list<>); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +static_assert(unique_event_ptrs == list); +``` + +> https://godbolt.org/z/rjh6hhK6j + +