-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path251.md
114 lines (85 loc) · 3.96 KB
/
251.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<details open><summary>Info</summary><p>
* **Did you know that C++20 added `type_identity` which implements the identity metafunction?**
* http://wg21.link/p0887
</p></details><details open><summary>Example</summary><p>
```cpp
template <class T>
void print1() {
std::cout << typeid(T).name() << std::endl;
}
template <class T>
void print2() {
std::cout << typeid(std::type_identity<T>).name() << std::endl;
}
int main() {
print1<int const&>(); // 'int'
print2<int const&>(); // 'type_identity<int const &>'
}
```
> https://godbolt.org/z/WWKh3673v
</p></details><details open><summary>Puzzle</summary><p>
* **Can you add required changes to tests and implement `overload_args` function which returns sum of sizes of passed arguments?**
```cpp
template<class...>
struct not_constructible {
not_constructible() = delete;
};
constexpr auto overload_args_sum(...); // TODO
static_assert(0u == overload_args_sum(not_constructible{}/*TODO*/));
static_assert(sizeof(int) == overload_args_sum(not_constructible<int>{}/*TODO*/));
static_assert(sizeof(int) + sizeof(float) == overload_args_sum(not_constructible<int, float>{}/*TODO*/));
static_assert(sizeof(int) + sizeof(float) + sizeof(char) == overload_args_sum(not_constructible<int, float, char>{}/*TODO*/));
```
> https://godbolt.org/z/8Y1qPrKMj
</p></details><details><summary>Solutions</summary><p>
```cpp
template<template<typename ...> class whatever, typename ... Ts>
constexpr auto overload_args_sum(whatever<not_constructible<Ts ...>>){
return (sizeof(Ts) + ... + 0);
}
static_assert(0u == overload_args_sum(std::type_identity<not_constructible<>>{}));
static_assert(sizeof(int) == overload_args_sum(std::type_identity<not_constructible<int>>{}));
static_assert(sizeof(int) + sizeof(float) == overload_args_sum(std::type_identity<not_constructible<int, float>>{}));
static_assert(sizeof(int) + sizeof(float) + sizeof(char) == overload_args_sum(std::type_identity<not_constructible<int, float, char>>{}));
```
> https://godbolt.org/z/WWExM6Pfo
```cpp
template <class... Ts>
constexpr auto overload_args_sum(std::type_identity<not_constructible<Ts...>>) -> std::size_t {
return (0 + ... + sizeof(Ts));
}
static_assert(0u == overload_args_sum(std::type_identity<not_constructible<>>{}));
static_assert(sizeof(int) == overload_args_sum(std::type_identity<not_constructible<int>>{}));
static_assert(sizeof(int) + sizeof(float) == overload_args_sum(std::type_identity<not_constructible<int, float>>{}));
static_assert(sizeof(int) + sizeof(float) + sizeof(char) == overload_args_sum(std::type_identity<not_constructible<int, float, char>>{}));
```
> https://godbolt.org/z/Wjnnh3n1b
```cpp
template <class... Ts>
using type_list_t = std::type_identity<not_constructible<Ts...>>;
template <class... Ts>
[[nodiscard]] constexpr auto overload_args_sum(
const type_list_t<Ts...>) noexcept {
return (0 + ... + sizeof(Ts));
}
static_assert(0u ==
overload_args_sum(std::type_identity<not_constructible<>>{}));
static_assert(sizeof(int) ==
overload_args_sum(std::type_identity<not_constructible<int>>{}));
static_assert(
sizeof(int) + sizeof(float) ==
overload_args_sum(std::type_identity<not_constructible<int, float>>{}));
static_assert(sizeof(int) + sizeof(float) + sizeof(char) ==
overload_args_sum(
std::type_identity<not_constructible<int, float, char>>{}));
```
> https://godbolt.org/z/sdhrEqKf6
```cpp
template<template<typename ... Args> class C, typename ... Args>
constexpr auto overload_args_sum(C<Args...>* c) { return (sizeof(Args)+...+0); }
static_assert(0u == overload_args_sum((not_constructible<>*)(nullptr) ));
static_assert(sizeof(int) == overload_args_sum((not_constructible<int>*)(nullptr) ));
static_assert(sizeof(int) + sizeof(float) == overload_args_sum((not_constructible<int, float>*)(nullptr)));
static_assert(sizeof(int) + sizeof(float) + sizeof(char) == overload_args_sum((not_constructible<int, float, char>*)(nullptr)));
```
> https://godbolt.org/z/oqaMf7fxa