Skip to content

Commit 15399ca

Browse files
committed
C++20 非型テンプレートパラメータとしてクラス型を許可する : プレースホルダー型とリテラル演算子の例を追加
#1273 (comment)
1 parent 8168531 commit 15399ca

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

GLOBAL_QUALIFY_LIST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* <algorithm>[link /reference/algorithm.md]
1010
* ExecutionPolicy[link /reference/execution/execution/execution_policy.md]
1111
* std::copy[link /reference/algorithm/copy.md]
12+
* std::copy_n[link /reference/algorithm/copy_n.md]
1213
* std::for_each[link /reference/algorithm/for_each.md]
1314
* std::sort[link /reference/algorithm/sort.md]
1415
* <any>[link /reference/any.md]

lang/cpp20/class_types_in_non-type_template_parameters.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ C++11以降での[汎用的な定数式`constexpr`](/lang/cpp11/constexpr.md)の
4444

4545

4646
##
47+
### floatやクラス型を非型テンプレートパラメータにする例
4748
```cpp example
4849
#include <utility>
4950

@@ -105,10 +106,79 @@ int main() {
105106
}
106107
```
107108
108-
### 出力
109+
#### 出力
109110
```
110111
```
111112
113+
### プレースホルダーを含む型を非型テンプレートパラメータにする例
114+
```cpp example
115+
#include <iostream>
116+
117+
template <class T>
118+
struct X {
119+
constexpr X(T x) : value(x) {}
120+
T value;
121+
};
122+
123+
template <X x>
124+
struct Y {
125+
void print() {
126+
std::cout << x.value << std::endl;
127+
}
128+
};
129+
130+
int main() {
131+
// Xクラステンプレートのテンプレートパラメータが
132+
// コンストラクタ引数`3`から推論される
133+
Y<3>{}.print();
134+
}
135+
```
136+
137+
#### 出力
138+
```
139+
3
140+
```
141+
142+
143+
### 文字列クラスオブジェクトを非型テンプレートパラメータにしてリテラル演算子を定義する例
144+
```cpp example
145+
#include <iostream>
146+
#include <algorithm>
147+
148+
template <typename CharT, std::size_t N>
149+
struct basic_fixed_string {
150+
constexpr basic_fixed_string(const CharT (&str)[N+1])
151+
{ std::copy_n(str, N+1, data); }
152+
153+
friend auto operator<=>(const basic_fixed_string&, const basic_fixed_string&) = default;
154+
CharT data[N+1];
155+
};
156+
157+
template <typename CharT, std::size_t N>
158+
basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N-1>;
159+
160+
template <std::size_t N>
161+
using fixed_string = basic_fixed_string<char, N>;
162+
163+
namespace my_literals {
164+
template <basic_fixed_string str>
165+
auto operator""_udl() {
166+
return str;
167+
}
168+
}
169+
170+
int main() {
171+
using namespace my_literals;
172+
auto s = "hello"_udl;
173+
std::cout << s.data << std::endl;
174+
}
175+
```
176+
177+
#### 出力
178+
```
179+
hello
180+
```
181+
112182
113183
## 備考
114184
- [Bug 97930 - `pair` is not a structural type](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97930)
@@ -121,4 +191,4 @@ int main() {
121191
122192
## 参照
123193
- [P0732R2 Class Types in Non-Type Template Parameters](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0732r2.pdf)
124-
- [P1907R1 Inconsistencies with non-type template parameters](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1907r1.html)
194+
- [P1907R1 Inconsistencies with non-type template parameters](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1907r1.html)

0 commit comments

Comments
 (0)