Skip to content

Commit

Permalink
[300] - Did you know that C++23 added support for constexpr std::bitset?
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Oct 16, 2022
1 parent 170e7c2 commit 5a226a7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

### Tips

* [[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 immplicit conversions?
* [[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()?
* [[297]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/297.md) - Did you know that C++20 introduced coroutines? (co_await)
* [[296]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/296.md) - Did you know that C++20 introduced coroutines? (co_yield)
Expand Down
94 changes: 86 additions & 8 deletions tips/299.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<details open><summary>Info</summary><p>

* **Did you know that C++20 concepts can be used to avoid immplicit conversions?**
* **Did you know that C++20 concepts can be used to avoid implicit conversions?**

* https://eel.is/c++draft/concept.same

Expand Down Expand Up @@ -36,7 +36,7 @@ struct {
auto foo(int, double, float) -> void;
constexpr auto can_invoke_with = []<class R, class... Ts>(R fn(Ts...), auto&&... args) {
return requires { { invoke.template operator[]<R, Ts...>(foo, std::forward<decltype(args)>(args)...) } -> std::same_as<R>; };
return requires { { invoke.template operator[]<R, Ts...>(fn, std::forward<decltype(args)>(args)...) } -> std::same_as<R>; };
};
static_assert( can_invoke_with(foo, int{}, double{}, float{}));
Expand All @@ -54,13 +54,91 @@ static_assert(not can_invoke_with(foo, int{}, double{}, float{}, float{}));
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](R fn(Ts...), std::same_as<Ts> auto... ts ) -> R;
```
> https://godbolt.org/z/T4rvx87Wo
```cpp
template<class R, class... Ts, class ...Ts2, ::std::enable_if_t<
sizeof...(Ts) == sizeof...(Ts2) &&
(::std::is_same_v<Ts, Ts2> && ...), int> = 0>
[[nodiscard]] constexpr auto operator[](R fn(Ts...), Ts2... ts ) -> R;
template<class R, class... Ts, class ...Ts2, ::std::enable_if_t<
sizeof...(Ts) == sizeof...(Ts2) &&
(::std::is_same_v<Ts, Ts2> && ...), int> = 0>
[[nodiscard]] constexpr auto operator[](R fn(Ts...), Ts2... ts ) -> R;
```
> https://godbolt.org/z/7fecsaErf

```cpp
struct {
template <class R, class... Ts>
[[nodiscard]] constexpr auto operator[](R fn(Ts...),
std::same_as<Ts> auto&&... args)
-> std::same_as<R> decltype(auto) {
return fn(std::forward<Ts>(args)...);
}
} invoke;
```

> https://godbolt.org/z/cbGrTW74q
```cpp
struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](R fn(Ts...), std::same_as<Ts> auto&&... args){
return fn(std::forward<decltype(args)>(args)...);
}
} invoke;
```

> https://godbolt.org/z/53jaMTK6z
```cpp
struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](auto&& fn, std::same_as<Ts> auto&&... args) -> std::same_as<R> auto {
return fn(std::forward<Ts>(args)...);
}
} invoke;
```

> https://godbolt.org/z/Gjv51ev13
```cpp
struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](auto fn, std::same_as<Ts>auto ...params) -> std::same_as<R> auto {
return fn(params...);
};
} invoke;
```

> https://godbolt.org/z/9vMh8sje6
```cpp
struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](auto F, auto... args) -> std::same_as<R> auto requires (std::same_as<Ts,decltype(args)> && ...) {
return F(args...);
};
} invoke;
```

> https://godbolt.org/z/TjbzWd7dT
```cpp

struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](R fn(Ts...), std::same_as<Ts> auto&& ... args)->R;
} invoke;
```

> https://godbolt.org/z/bz38Yb3c6
```cpp
struct {
template<class R, class... Ts>
[[nodiscard]] constexpr auto operator[](auto f, std::same_as<Ts> auto &&...args){
f(args...);
}
} invoke;
```
> https://godbolt.org/z/MP9nK16xr
43 changes: 43 additions & 0 deletions tips/300.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<details open><summary>Info</summary><p>

* **Did you know that C++23 added support for constexpr std::bitset?**

* https://eel.is/c++draft/bitset

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

```cpp
#include <bitset>

constexpr std::bitset<4> b1{"0011"};
static_assert(0b0011 == b1.to_ulong());

constexpr std::bitset<4> b2{0b0011};
static_assert(b1 == b2);
```
> https://godbolt.org/z/vo13Kf9Er
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement consteval `to_string` which converts bitset to its string representation?**
```cpp
[[nodiscard]] consteval auto to_string(auto bitset); // TODO
static_assert(std::string_view{"0"} != to_string(std::bitset<1>{"1"}));
static_assert(std::string_view{"01"} != to_string(std::bitset<1>{"10"}));
static_assert(std::string_view{"111"} != to_string(std::bitset<1>{"000"}));
static_assert(std::string_view{"0"} == to_string(std::bitset<1>{"0"}));
static_assert(std::string_view{"1"} == to_string(std::bitset<1>{"1"}));
static_assert(std::string_view{"00"} == to_string(std::bitset<2>{"00"}));
static_assert(std::string_view{"01"} == to_string(std::bitset<2>{"01"}));
static_assert(std::string_view{"10"} == to_string(std::bitset<2>{"10"}));
static_assert(std::string_view{"11"} == to_string(std::bitset<2>{"11"}));
static_assert(std::string_view{"100"} == to_string(std::bitset<3>{"100"}));
```

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

0 comments on commit 5a226a7

Please sign in to comment.