teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コメントへの回答

2018/04/30 02:29

投稿

Chironian
Chironian

スコア23274

answer CHANGED
@@ -37,4 +37,59 @@
37
37
  2 4 6
38
38
  4 8 12
39
39
  ```
40
- [wandbox](https://wandbox.org/permlink/n8mQw23IGqPjZwJZ)
40
+ [wandbox](https://wandbox.org/permlink/n8mQw23IGqPjZwJZ)
41
+
42
+ ---
43
+ 【コメントに対する回答】
44
+ print()を再帰定義してみました。
45
+
46
+ ```C++
47
+ #include <iostream>
48
+
49
+ template<int... kArgs>
50
+ class Foo
51
+ {
52
+ template<typename...>
53
+ void printImpl()
54
+ {
55
+ }
56
+
57
+ template<int kFirst, int... kRest>
58
+ void printImpl()
59
+ {
60
+ std::cout << kFirst << " ";
61
+ printImpl<kRest...>();
62
+ }
63
+
64
+ public:
65
+ void print()
66
+ {
67
+ printImpl<kArgs...>();
68
+ std::cout << "\n";
69
+ }
70
+ Foo<(kArgs*2)...> map_double() const
71
+ {
72
+ return Foo<(kArgs*2)...>();
73
+ }
74
+ };
75
+
76
+ int main()
77
+ {
78
+ Foo<>().print();
79
+ Foo<1>().print();
80
+ Foo<1, 2>().print();
81
+
82
+ Foo<1, 2, 3>().print();
83
+ Foo<1, 2, 3>().map_double().print();
84
+ Foo<1, 2, 3>().map_double().map_double().print();
85
+ }
86
+ ```
87
+
88
+ > template<typename...>
89
+ void printImpl()
90
+ {
91
+ }
92
+
93
+ にて再帰の最後でテンプレート引数が無くなった時、および、テンプレート実引数が0個の時(Foo<>)に対応してみました。「曖昧」にならないかちょっと心配ですが、gcc, clang, msvcで通りましたし、[MSDN](https://msdn.microsoft.com/ja-jp/library/zaycz069.aspx)のこの記述を見る限り使って良さそうです。
94
+
95
+ [wandbox](https://wandbox.org/permlink/FNtz7ccRgnKDeHCc)

1

余分なので削除

2018/04/30 02:29

投稿

Chironian
Chironian

スコア23274

answer CHANGED
@@ -37,6 +37,4 @@
37
37
  2 4 6
38
38
  4 8 12
39
39
  ```
40
- [wandbox](https://wandbox.org/permlink/n8mQw23IGqPjZwJZ)
40
+ [wandbox](https://wandbox.org/permlink/n8mQw23IGqPjZwJZ)
41
-
42
- 結構難易度の高い領域ですので、たいへんと思いますが頑張って下さい。