Monday, 18 September 2017

c++ - Compiling error on template method, return is instance from inner class





Here is a simplified example:



template
class MyTemplate
{
class Inner {};
Inner met();
};

template

MyTemplate::Inner MyTemplate::met()
{ }


I get the following compilation error:



expected constructor, destructor, or type conversion before 'met'


I use GCC. It seems the compiler doesn't recognize MyTemplate::Inner as a proper class. How can I fix this? I've tried sticking the typename keyword here and there to no avail. Right now, the only way I can manage to compile this is to inline the method definition in the class declaration, which I would like to avoid.



Answer



Clang reports the following:



error: missing 'typename' prior to dependent type name
'MyTemplate::Inner' MyTemplate::Inner MyTemplate::met()
^~~~~~~~~~~~~~~~~~~~ typename 1 error generated.


And placing typename in the appropriate place fixes it.




template
class MyTemplate
{
class Inner {};
Inner met();
};

template
typename MyTemplate::Inner MyTemplate::met()
{ }



Did you put typename in the correct location? If so, then this must be a bug in G++.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...