diff --git a/README.md b/README.md index 90fe411..de74331 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ ### Tips +* [[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? diff --git a/tips/331.md b/tips/331.md index 1ddad44..d21b6b0 100644 --- a/tips/331.md +++ b/tips/331.md @@ -2,6 +2,8 @@ * **Did you about C++17 std::index_sequence, std::make_index_sequence?** + * https://eel.is/c++draft/utility.syn#lib:index_sequence +

Example

```cpp @@ -51,4 +53,70 @@ static_assert(matrix[2][2]); > https://godbolt.org/z/3YvfqezPa

Solutions

+ +```cpp +template > +constexpr const std::array, N> matrix{}; + +template +constexpr const std::array matrix>{ + [] -> std::array { + return {[] -> int { + return requires(T v, id p) { v.bar(p); }; + }.template operator()()...}; + }.template operator()()...}; +``` + +> https://godbolt.org/z/Yq9xdYo3a + +```cpp +template +constexpr auto has = []() { return requires(T t) { t.bar(id{}); }; }; + +template +constexpr auto row = []() { + constexpr auto make_row = + [](std::index_sequence) -> std::array { + return {has()...}; + }; + + return make_row(std::make_index_sequence{}); +}; + +template +constexpr auto rows = []() { + constexpr auto make_rows = [](std::index_sequence) + -> std::array, N> { return {row()...}; }; + + return make_rows(std::make_index_sequence{}); +}; + +template +constexpr const auto matrix = rows(); +``` + +> https://godbolt.org/z/Gcx4sd5E3 + +```cpp +template +concept hasBar = requires(T t) { + { t.bar(std::declval>()) }; +}; + +template +constexpr std::array, N> make_symmetric_matrix( + Args&&... args) { + return {std::forward(args)...}; +} + +template +constexpr const auto matrix = + [](std::index_sequence) + -> std::array, N> { + return make_symmetric_matrix(hasBar(Indices)>...); +}(std::make_index_sequence{}); +``` + +> https://godbolt.org/z/bdjEadn5f +

diff --git a/tips/332.md b/tips/332.md new file mode 100644 index 0000000..4290e9f --- /dev/null +++ b/tips/332.md @@ -0,0 +1,43 @@ +
Info

+ +* **Did you know that in C++ you can generate jump tables at compile-time?** + + * https://en.wikipedia.org/wiki/Branch_table + +

Example

+ +```cpp +template constexpr auto foo() { return N; } + +constexpr std::array jump_table{ + foo<0>, + foo<1>, + foo<2>, +}; + +static_assert(0 == jump_table[0]()); +static_assert(1 == jump_table[1]()); +static_assert(2 == jump_table[2]()); +``` + +> https://godbolt.org/z/x3xa9erGE + +

Puzzle

+ +* **Can you implemnt dispatch fn which generates jump table for given N?** + +```cpp +template constexpr auto foo() { return N; } + +template +constexpr auto dispatch(auto n); // TODO + +static_assert(1 == dispatch(1)); +static_assert(7 == dispatch(7)); +static_assert(23 == dispatch(23)); +``` + +> https://godbolt.org/z/4M9x1vjcG + +

Solutions

+