10

I have inherited a project making heavy use of template meta programming, and am now in the process of upgrading from Visual Studio 2010 to 2012. Some of the template-code no longer works in 2012. I have distilled a minimal example:

template <typename T, int i>
class MyClass
{
private:
    typedef typename T::Nested<i> Found;
};

Gives this error message:

    source.cpp(5): error C2059: syntax error : '<'
    source.cpp(6) : see reference to class template instantiation 'MyClass<T,i>' being compiled
    source.cpp(5): error C2238: unexpected token(s) preceding ';'

Further down in MyClass, I can use T::Nested<i>, it is just the typedef that does not work.

This example compiles in 2010, but not in 2012. What is wrong with this code?

2
  • 7
    Not 100% (thus not an answer) but I believe you are missing a template: typedef typename T::template Nested<i> Found; or something alike. Commented Oct 1, 2012 at 13:11
  • 1
    Just adding to David's info, it's always a good idea to expose Visual C++ template code to g++. And in general, to make the code compile with at least two different compilers. In the old days folks who were really serious about it used Comeau, but I'm not sure whether it's been updated to more full C++11 conformance. Commented Oct 1, 2012 at 13:14

1 Answer 1

12

Each VS version is more and more strict about requiring template and typename. You're missing a template, and VS2012 is right to complain.

1
  • 1
    You might point out where. (before Nested, since the compiler cannot guess that T::Nested is a template until the template is instantiated)
    – MSalters
    Commented Oct 1, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.