2

以下は、g++ 4.7.1 でコンパイルに失敗するコードの要点です。

#include <iostream>
using namespace std;

template <typename T> void bottom(T x) {cout << x << " ";}

template <typename Head, typename Tail...> 
void recurse(Head h, Tail t) {bottom(h); recurse(t...)}

void recurse(){}

int main() { recurse(1,2.2); }

理由は不明ですが、「void recurse(){}」はテンプレートの再帰に参加していません。

手がかりを探しています。

4

2 に答える 2

4

There are a few syntactic problems with that code (I doubt that you copy-pasted as is from Bjarne's book), but after fixing them, it seems the main problem is that the overload of recurse() accepting no arguments appears only after the function template recurse().

Moving it before it fixes the problem:

#include <iostream>

using namespace std;

template <typename T>
void bottom(T x) {cout << x << " ";}

void recurse(){} // <== MOVE THIS BEFORE THE POINT WHERE IT IS CALLED

template <typename Head, typename... Tail>
void recurse(Head h, Tail... t)
{
    bottom(h);
    recurse(t...);
}

int main() { recurse(1,2.2,4); }

Here is a live example.

于 2013-05-25T14:45:32.640 に答える