Sunday, 10 December 2017

c++ using nested template classes for carrying type information












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 struct Container

{
typedef vector type;


};
};

template
class
Consumer
{
typedef typename Provider::Container::type
intContainer;
typedef typename Provider::Container::type
doubleContainer;
};



There
is a workaround for it but i just wonder if it should be
required:



struct TypeProvider

{
template struct Container { typedef
vector type; };
};

template T> class Container, class Obj>
struct
Type

{
typedef typename Container::type
type;
};

template Provider>
class TypeConsumer
{
typedef typename
Type::type intContainer;
typedef typename
Type::type
doubleContainer;
};



Answer




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



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



This
is very well explained in the accepted answer to href="https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords">this
SO post.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...