質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

2965閲覧

C++のtemplateを使いたいのですが

stmtk_01

総合スコア7

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

1クリップ

投稿2018/04/29 10:55

前提・実現したいこと

map_doubleという関数を使って全ての要素を2倍にしたいのですが、うまくいきません。

c++

1#include <iostream> 2 3using namespace std; 4 5 6template<int head = 0, int... tail> 7class Turing{ 8public: 9 template <int new_head> 10 Turing<new_head, head, tail...> cons(){ 11 return Turing<new_head, head, tail...>(); 12 } 13 14 void print(){ 15 cout << head << " " << endl; 16 if(sizeof...(tail) != 0) Turing<tail...>().print(); 17 } 18 19 auto map_double(){ 20 cout << head << " " << endl; 21 if(sizeof...(tail) != 0){ 22 return Turing<tail...>().map_double().cons<head * 2>(); 23 } 24 return Turing<head * 2>(); 25 } 26}; 27 28int main(){ 29 auto a = Turing<1, 2, 3>().map_double().print(); 30 return 0; 31}

発生している問題・エラーメッセージ

template_test.cpp:22:66: error: expected expression return Turing<tail...>().map_double().cons<head * 2>(); ^ 1 error generated.

補足情報(FW/ツールのバージョンなど)

Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

expected expression の直接的な原因は、メンバ関数呼び出しの部分のテンプレート引数が正しくテンプレート引数として扱われていないことにあります。こういう場合、メンバ関数テンプレート名の前に template をつける必要があります。

cpp

1map_double().template cons<head*2>();

どうパースされているのかまでは私には分からないのですが、不等号か何かかと思っているのでしょうね?

この記法を使って修正したコードが次です。
なお、 if constexpr 文は tail の数が 0 でないときはそもそも Turing<tail...> を展開しないようにするために利用しています。これにより template <int head = 0, ...> のような無意味なデフォルト引数が必要なくなります。 (無意味でなかったならごめんなさい) 。

cpp

1#include <iostream> 2 3using namespace std; 4 5template<int head, int... tail> 6struct Turing { 7 template <int new_head> 8 Turing<new_head, head, tail...> cons() const { 9 return Turing<new_head, head, tail...>(); 10 } 11 12 void print() { 13 cout << head << " " << endl; 14 if constexpr (sizeof...(tail) != 0) 15 Turing<tail...>().print(); 16 } 17 18 auto map_double() { 19 if constexpr (sizeof...(tail) != 0){ 20 return Turing<tail...>().map_double().template cons<head*2>(); 21 } else { 22 return Turing<head * 2>(); 23 } 24 } 25}; 26 27int main(){ 28 auto a = Turing<1, 2, 3, 4>().map_double(); 29 a.print(); 30 return 0; 31}

投稿2018/04/30 03:00

編集2018/04/30 03:45
Eki

総合スコア429

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

stmtk_01

2018/04/30 06:27

そうですね。まさにこんな感じのコードが書きたかったんです。 constexprすごいですね。ありがとうございます
guest

0

こんにちは。

外しているかもしれませんが、テンプレート・パラメータを倍にして表示してみました。

C++

1#include <iostream> 2 3template<int... kArgs> 4struct Foo 5{ 6 void print() 7 { 8 using swallow = int[]; 9 (void)swallow 10 { 11 (std::cout << kArgs << " ", 0)... 12 }; 13 std::cout << "\n"; 14 } 15 Foo<(kArgs*2)...> map_double() const 16 { 17 return Foo<(kArgs*2)...>(); 18 } 19}; 20 21int main() 22{ 23 Foo<1, 2, 3>().print(); 24 Foo<1, 2, 3>().map_double().print(); 25 Foo<1, 2, 3>().map_double().map_double().print(); 26}

実行結果:

1 2 3 2 4 6 4 8 12

wandbox


【コメントに対する回答】
print()を再帰定義してみました。

C++

1#include <iostream> 2 3template<int... kArgs> 4class Foo 5{ 6 template<typename...> 7 void printImpl() 8 { 9 } 10 11 template<int kFirst, int... kRest> 12 void printImpl() 13 { 14 std::cout << kFirst << " "; 15 printImpl<kRest...>(); 16 } 17 18public: 19 void print() 20 { 21 printImpl<kArgs...>(); 22 std::cout << "\n"; 23 } 24 Foo<(kArgs*2)...> map_double() const 25 { 26 return Foo<(kArgs*2)...>(); 27 } 28}; 29 30int main() 31{ 32 Foo<>().print(); 33 Foo<1>().print(); 34 Foo<1, 2>().print(); 35 36 Foo<1, 2, 3>().print(); 37 Foo<1, 2, 3>().map_double().print(); 38 Foo<1, 2, 3>().map_double().map_double().print(); 39}

template<typename...>

void printImpl()
{
}

にて再帰の最後でテンプレート引数が無くなった時、および、テンプレート実引数が0個の時(Foo<>)に対応してみました。「曖昧」にならないかちょっと心配ですが、gcc, clang, msvcで通りましたし、MSDNのこの記述を見る限り使って良さそうです。

wandbox

投稿2018/04/29 13:02

編集2018/04/30 02:29
Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

stmtk_01

2018/04/30 00:32

こんな書き方できるなんて知りませんでした。これを参考にしてQiitaで記事を書きますね ちなみにこれを再帰で書く方法も知りませんか?
Chironian

2018/04/30 02:21

Fooの展開を再帰で書くのは虚しすぎるので、printを再帰定義してみます。
yumetodo

2018/04/30 03:37

こういうのこそif constexprが使えるんじゃないかという思い
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問