C ++ using nested template classes to transfer information like

Possible duplicate:
Where and why do I need to put the keywords "template" and "typename"?

When I try to compile the following code in VS 2012, I get errors in the typedef lines of the Consumer class, starting with:

error C2143: syntax error : missing ';' before '<' 

Is this a compiler problem or is the code no longer valid with C ++? (The project is extracted from, of course, used to build without problems in older versions of VS - and gcc iirc - but that was about 10 years ago!)

 struct TypeProvider { template<class T> struct Container { typedef vector<T> type; }; }; template<class Provider> class Consumer { typedef typename Provider::Container<int>::type intContainer; typedef typename Provider::Container<double>::type doubleContainer; }; 

There is a workaround for it, but I'm just wondering if this is needed:

 struct TypeProvider { template<typename T> struct Container { typedef vector<T> type; }; }; template<template<class T> class Container, class Obj> struct Type { typedef typename Container<Obj>::type type; }; template<typename Provider> class TypeConsumer { typedef typename Type<Provider::Container, int>::type intContainer; typedef typename Type<Provider::Container, double>::type doubleContainer; }; 
+4
source share
1 answer

You need to help the compiler know that the Container is a template:

 template<class Provider> class Consumer { typedef typename Provider:: template Container<int>::type intContainer; typedef typename Provider:: template Container<double>::type doubleContainer; }; 

This is well explained in the accepted answer to this SO message .

+8
source

All Articles