Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting the error "non-template 'f' used as template"

I'm trying to understand where to use template and typename and I came across a problem I can't quite get around. I have a template-function f<T> which uses the type passed to it (which will be a class) to call the template-member-function .f<T>. I think my use of typename in the function body is correct, however, I keep getting the following error:

source.cpp: In function 'void f()':
source.cpp:11:19: error: non-template 'f' used as template
source.cpp:11:19: note: use 'typename T::C::template f' to indicate that it is a template

struct A {
  struct C {
     template <typename T> void f() {}
  };
};

template <typename T> void f() {

  typename T::C::f<int>();

}

int main() {

  f<A>();

}

Notice how on the last error it advises to use 'typename T::C::template f' instead. So I made the following change in accordance:

// ...
typename T::C::template f<int>();
// ...

I did as it said, but then I received the next line of error:

error: no class template named 'f' in 'struct A::C'

I believe this error is incorrect in that there is in fact a public template function named f in struct A::C. What could I be doing wrong here?

like image 497
template boy Avatar asked Oct 18 '12 16:10

template boy


1 Answers

Assuming we make f static so it can be called without an instance, you don't need typename because you're not creating any ambiguities with a dependent type (i.e. C can't be a variable because you use it with :: just after). Here is the correct syntax:

struct A {
  struct C {
     template <typename T>
     static void f() {}
  };
};

template <typename T> void f() {
  T::C::template f<int>();
}

If you wanted not to make it static, you'd create a A::C and use .template f<int>():

struct A {
  struct C {
     template <typename T>
     static void f() {}
  };
};

template <typename T> void f() {
  typename T::C c;
//^^^^^^^^ Now we DO need typename to disambiguate.

  c.template f<int>();
}
like image 111
Seth Carnegie Avatar answered Nov 07 '22 19:11

Seth Carnegie