-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path341.md
49 lines (35 loc) · 1.07 KB
/
341.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<details open><summary>Info</summary><p>
* **Did you know that C++26 added user-generated static_assert messages?**
* https://wg21.link/P2741
</p></details><details open><summary>Example</summary><p>
```cpp
static_assert(false, std::string_view{"message"});
```
> https://godbolt.org/z/njoWdn7T7
</p></details><details open><summary>Puzzle</summary><p>
* **Can you apply format for static_assert messages?**
```cpp
// TODO format
struct foo {};
static_assert(sizeof(foo) == 0, format("Unexpected sizeof: expected 0, got {}"_s, sizeof(foo)));
```
> https://godbolt.org/z/9scM35GzP
</p></details>
</p></details><details><summary>Solutions</summary><p>
```cpp
template<auto... Cs>
constexpr auto format(const string<Cs...> fmt, auto&&... args) {
std::array<char, sizeof...(Cs)> a{};
for (auto i = 0; i < sizeof...(Cs); ++i) {
if (fmt.str[i] == '{' and fmt.str[i+1] == '}') {
a[i] = '0'+(args,...);
a[++i] = ' ';
} else {
a[i] = fmt.str[i];
}
}
return a;
};
```
> https://godbolt.org/z/oMd7aMqKz
</p></details>