1

Why won't the commented-line in goo compile? Instead I have to resort to defining the global function hoo instead of using the Thing member function foo?

#include <iostream>

template <typename... T>
struct Thing {
    template <typename U> void foo() {std::cout << this << '\n';}
};

template <typename U, typename... T>
void hoo (const Thing<T...>& thing) {std::cout << &thing << '\n';}

template <typename U, typename... T>
void goo (const Thing<T...>& thing) {
//  thing.foo<U>();  // Why won't this line compile?
    hoo<U>(thing);  // Using this instead.
}

int main() {
    Thing<int, double, char> thing;
    goo<short>(thing);
}

What is the change that is needed so that I can use foo() instead?

prestokeys
  • 4,817
  • 3
  • 20
  • 43

1 Answers1

2

On the line thing.foo<U>() the compiler doesn't have enough information to tell if foo is template or not. Use the template keyword to disambiguate this:

thing.template foo<U>();
David G
  • 94,763
  • 41
  • 167
  • 253