@@ -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