Using template parameters as template parameters

Why is the following code invalid?

template <typename S, typename T>
struct B{
    void f(T t, S s) {t.f<S>(s); }
};

gcc 4.3.4 complains that this is the "expected primary expression before"> the token, i.e. that "S" is not a valid base expression.

+2
source share
2 answers

You need to specify what fis the pattern:

void f(T t, S s) {
    t.template f<S>(s);
}

++ ( ), f s T. , : < , ? ++ , , f , ++ , T.

+12

, , . "tf (s)", : , , , f , , f, S.

+1

All Articles