Skip to content

Commit

Permalink
[335] - Did you know that you can simplify boost.mp11 API with DSL?
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Jun 18, 2023
1 parent 5ee533b commit 1f910a8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
15 changes: 8 additions & 7 deletions tips/334.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ static_assert(typeid(unsigned long long) == typeid(call(1ll, 2ull, 3l)));
</p></details><details><summary>Solutions</summary><p>

```cpp
#include <functional>
#include <type_traits>

constexpr auto sum(auto... ts) { return (ts + ...); }

[[nodiscard]] constexpr auto call(auto... ts) {
using Ret = std::common_type_t<decltype(ts)...>;
return std::invoke_r<Ret>(sum<decltype(ts)...>, ts...);
Expand All @@ -55,7 +50,13 @@ constexpr auto sum(auto... ts) { return (ts + ...); }
> https://godbolt.org/z/1baMcc331
</p></details>
<details><summary>Solutions</summary><p>
```cpp
[[nodiscard]] constexpr auto call(auto... ts){
using T = typename std::common_type_t<decltype(ts)...>;
return std::invoke_r<T>(&sum<decltype(ts)...>, ts...);
}
```

> https://godbolt.org/z/xcn9qnrcY
</p></details>
61 changes: 61 additions & 0 deletions tips/335.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<details open><summary>Info</summary><p>

* Did you know that you can simplify `boost.mp11` API with DSL*?

* https://github.com/boostorg/mp11
* DSL - Domain Specific Language

</p></details><details open><summary>Example</summary><p>

```cpp
template <class T1, class T2>
[[nodiscard]] constexpr auto operator==(const T1&, const T2&) { return std::is_same_v<T1, T2>; }

template <class... Ts>
constexpr auto list = mp11::mp_list<Ts...>{};

static_assert(list<> == list<>);
static_assert(list<int> == list<int>);
static_assert(list<int, double> == list<int, double>);
```
> https://godbolt.org/z/fTMsK8Moh
</p></details><details open><summary>Puzzle</summary><p>
* 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<class... Ts>
constexpr auto unique_event_ptrs =
list<Ts...>
| unique
| filter<is_base_of<event>>
| transform<add_pointer>
;
static_assert(unique_event_ptrs<> == list<>);
static_assert(unique_event_ptrs<foo1> == list<foo1*>);
static_assert(unique_event_ptrs<foo1, foo1> == list<foo1*>);
static_assert(unique_event_ptrs<bar1> == list<>);
static_assert(unique_event_ptrs<bar1, bar1> == list<>);
static_assert(unique_event_ptrs<bar2, bar1> == list<>);
static_assert(unique_event_ptrs<foo2, foo1> == list<foo2*, foo1*>);
static_assert(unique_event_ptrs<foo1, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, foo2, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, foo1, foo2, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, foo2, foo1, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, bar1, foo1, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, bar1, foo1, foo2> == list<foo1*, foo2*>);
static_assert(unique_event_ptrs<foo1, bar1, foo1, foo1, bar1, bar2> == list<foo1*>);
```

> https://godbolt.org/z/rjh6hhK6j
</p></details>

0 comments on commit 1f910a8

Please sign in to comment.