Skip to content

Commit

Permalink
[316] - Did you know about std::rank/std::rank_v type_trait to get …
Browse files Browse the repository at this point in the history
…the rank of the array?
  • Loading branch information
kris-jusiak committed Feb 5, 2023
1 parent 29edba2 commit 2b73f42
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

### Tips

* [[315]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/315.md) - Did you about C++20 `is_layout_compatible_v` type_traits?
* [[316]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/316.md) - Did you know about `std::rank/std::rank_v` type_trait to get the rank of the array?
* [[315]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/315.md) - Did you know about C++20 `is_layout_compatible_v` type_traits?
* [[314]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/314.md) - Did you know that with gnu:C++26 a more parts of static reflection can be emulated?
* [[313]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/313.md) - Did you know that C++26 added #embed?
* [[312]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/312.md) - Did you know that C++20 added support for Unevaluated asm-declaration in constexpr functions?
Expand All @@ -24,9 +25,9 @@
* [[306]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/306.md) - Did you know about if/else hell anti-pattern?
* [[305]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/305.md) - Did you know about (rejected) proposal for homogeneous variadic function parameters?
* [[304]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/304.md) - Did you know that tuple can be implement just with lambdas?
* [[303]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/303.md) - Did you about typename erasure technique to reduce compilation times with templates?
* [[302]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/302.md) - Did you now that with concepts you can override a type?
* [[301]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/301.md) - Did you now that functions in <charconv> are constexpr since C++23?
* [[303]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/303.md) - Did you know about typename erasure technique to reduce compilation times with templates?
* [[302]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/302.md) - Did you know that with concepts you can override a type?
* [[301]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/301.md) - Did you know that functions in <charconv> are constexpr since C++23?
* [[300]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/300.md) - Did you know that C++23 added support for constexpr std::bitset?
* [[299]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/299.md) - Did you know that C++20 concepts can be used to avoid implicit conversions?
* [[298]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/298.md) - Did you know that C++23 added static operator()?
Expand Down
19 changes: 17 additions & 2 deletions tips/315.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<details open><summary>Info</summary><p>

* **Did you about C++20 `is_layout_compatible_v` type_trait?**
* **Did you know about C++20 `is_layout_compatible_v` type_trait?**

* https://eel.is/c++draft/meta.type.synop

Expand Down Expand Up @@ -58,10 +58,25 @@ static_assert(4 == count_compatible<int, int, const int, int const, unsigned con
> https://godbolt.org/z/7cjqdxGxE

</p></details><details><summary>Solutions</summary><p>

```cpp
template<class T, class... Ts>
constexpr auto count_compatible = (std::is_layout_compatible_v<T, Ts> + ... + std::size_t{});
```
> https://godbolt.org/z/8v741635E
```cpp
template<class T, class... Ts>
constexpr auto count_compatible = []{
return (std::is_layout_compatible_v<T, Ts> + ...);
}();
```

> https://godbolt.org/z/GEcr44h49

```cpp
template<class T, class... Ts>
constexpr auto count_compatible = (... + std::is_layout_compatible_v<T, Ts>);
```
> https://godbolt.org/z/fxqYeben5
41 changes: 41 additions & 0 deletions tips/316.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<details open><summary>Info</summary><p>

* Did you know about `std::rank/std::rank_v` type_trait to get the rank of the array?

* https://eel.is/c++draft/meta.unary.prop.query#lib:rank

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

```cpp
static_assert(0 == std::rank_v<void>);
static_assert(0 == std::rank_v<int>);
static_assert(1 == std::rank_v<int[]>);
static_assert(0 == std::rank_v<int[0]>);
static_assert(1 == std::rank_v<int[1]>);
static_assert(1 == std::rank_v<int[42]>);
static_assert(2 == std::rank_v<int[][2]>);
static_assert(2 == std::rank_v<int[1][2]>);
static_assert(3 == std::rank_v<int[1][2][3]>);
```
> https://godbolt.org/z/e657s3
</p></details><details open><summary>Puzzle</summary><p>
> Can you implement standard compliant version of `std::rank_v`?
```cpp
static_assert(0 == rank_v<void>);
static_assert(0 == rank_v<int>);
static_assert(1 == rank_v<int[]>);
static_assert(0 == rank_v<int[0]>);
static_assert(1 == rank_v<int[1]>);
static_assert(1 == rank_v<int[42]>);
static_assert(2 == rank_v<int[][2]>);
static_assert(2 == rank_v<int[1][2]>);
static_assert(3 == rank_v<int[1][2][3]>);
```

> https://godbolt.org/z/7Ms3W4TdP
</p></details><details><summary>Solutions</summary><p>

0 comments on commit 2b73f42

Please sign in to comment.