1

I have the following (simplified) code:

#include <iostream>

class Foo {
public:
  template<class T>
  static size_t f() {
    return sizeof(T);
  }
};

template<class A>
class Bar {
public:
  template<class B>
  static void f(B const& b) {
    std::cout << A::f<B>() << std::endl;
  }
};

int main() {
  Bar<Foo>::f(3.0);
  return 0;
}

It compiles fine in MSVC, but in GCC (5.2.1) it gives the following error:

main.cpp:16:26: error: expected primary-expression before ‘>’ token
     std::cout << A::f<B>() << std::endl;
                          ^

(followed by a few hundred lines of cout-related errors). I suppose it doesn't realize that A::f can be a template function? Is it breaking anything in the standard?

riv
  • 6,846
  • 2
  • 34
  • 63

1 Answers1

3

You need to put the keyword template:

std::cout << A::template f<B>() << std::endl;

You need to put it because A is a dependent name, otherwise the compiler could interpret it as a comparison operator:

A::f < B
Mattia F.
  • 1,720
  • 11
  • 21
  • 1
    Thanks, exactly what I was looking for. MSVC is very forgiving with these keywords :( (I'm guessing it doesn't even try to parse the function body until it needs to instantiate it). – riv Jul 04 '16 at 13:39