3

I am using Visual Studio 2013 and having some issues with a function returning a nested template class inside a template outer class. I have made a minimal example, the real one involves a lot more code:

template<typename R, typename... S>
class Foo
{
public:
    template<typename T>
    class Bar
    {

    };
};

template<typename T, typename R, typename... S>
typename Foo<R, S...>::Bar<T> fooBar() { // <--- LINE 33

}

This yields a whole set of errors (mostly from subsequent code):

  • 33: error C2988: unrecognizable template declaration/definition
  • 33: error C2059: syntax error : '<'

And it also affects subsequent code, tons of syntax error come for all the lines afterwards.

Am I not seeing something or could this be an issue of Visual Studio?

Cromon
  • 821
  • 10
  • 24
  • possible duplicate of [Where and why do I have to put the "template" and "typename" keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Constructor May 27 '14 at 17:18

1 Answers1

5

THE PROBLEM/SOLUTION

You are required to use the keyword template in this context to tell the compiler that Bar is indeed a template, as in the below snippet:

template<typename T, typename R, typename... S>
typename Foo<R, S...>::template Bar<T> fooBar() { // <--- LINE 33
   ...
}

BUT WHY?

We are required to use the template keyword whenever a template name is a dependent template name, without it the compiler will treat Bar in Foo<R, S...>::Bar as a non-template, which doesn't make sense; and it errors.

Further reading:

Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
  • 1
    I think you should add the line `return typename Foo::template Bar();` in the function to complete the example. – R Sahu May 27 '14 at 17:18
  • @RSahu We don't know if `Bar` is *default constructible*, instead I've added `...` to the example to show that the contents of the function is left out (by intention). – Filip Roséen - refp May 27 '14 at 17:21