diff --git a/README.md b/README.md index b057912..b22412b 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ > C++17 +* [Did you know that C++17 added Hardware interference size?](https://github.com/tip-of-the-week/cpp/blob/master/tips/369.md) * [Did you know that the underlying visit implementation of std::visit has changed since GCC12+, Clang15+?](https://github.com/tip-of-the-week/cpp/blob/master/tips/353.md) * [Did you know that run-time dispatching over type-list can be implemented many different ways?](https://github.com/tip-of-the-week/cpp/blob/master/tips/337.md) * [Did you know that you can simplify `boost.mp11` API with DSL?](https://github.com/tip-of-the-week/cpp/blob/master/tips/335.md) diff --git a/tips/369.md b/tips/369.md new file mode 100644 index 0000000..4628ecb --- /dev/null +++ b/tips/369.md @@ -0,0 +1,47 @@ +
Info

+ +* **Did you know that C++17 added Hardware interference size?** + + * https://wg21.link/P0154 + +

Example

+ +```cpp +#include +static_assert(64u == std::hardware_destructive_interference_size); // x86-64 +``` + +> https://godbolt.org/z/haPq9583q + +

Puzzle

+ +* **Can you implement `cache_aligned_array` function which returns cache size aligned std::array with provided values? + +```cpp +constexpr auto cache_aligned_array(auto... args) { + // TODO + return std::array{args...}; +}; + +static_assert(std::hardware_destructive_interference_size == alignof(cache_aligned_array(1, 2, 3))); +``` + +> https://godbolt.org/z/EvKco4fG7 + +

+ +

Solutions

+ +```cpp +constexpr auto cache_aligned_array(auto... args) { + struct alignas(std::hardware_destructive_interference_size) + : decltype(std::array{args...}) { } array {args...}; + return array; +}; + +static_assert(std::hardware_destructive_interference_size == alignof(cache_aligned_array(1, 2, 3))); +``` + +> https://godbolt.org/z/oTezYjsW4 + +