Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ using nested template classes for carrying type information [duplicate]

Tags:

c++

templates

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

When i try to compile the following code in VS 2012 i get errors on the Consumer class's typedef lines starting with:

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

Is this a problem with the compiler or is the code no longer valid c++? (The project it is extracted from certainly used to build without problems on 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 just wonder if it should be required:

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;
};
like image 544
ian.witz Avatar asked Jan 14 '23 09:01

ian.witz


1 Answers

You need to help the compiler know that 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 very well explained in the accepted answer to this SO post.

like image 142
juanchopanza Avatar answered Jan 16 '23 21:01

juanchopanza