Template Explicit Instantiation error

View: New views
4 Messages — Rating Filter:   Alert me  

Template Explicit Instantiation error

by GodfatherofSoul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to migrate some Visual Studio code to gcc, but I'm getting errors with some of our template usage:

class ParentClass
{
public:
  class NestedForwardDeclaration;

  template class SomeTemplateClass<NestedForwardDeclaration>;

  class NestedForwardDeclaration
  {
  ...
  };

};

BTW, this is distilled down to the core cause of the problem, so you really can't see the rationale for this convention.  The complication error I get is on the "template class.." line:

error: expected `<' before 'class'

Does anyone understand why this fails?  Note that this same code compiles and runs with the Microsoft compiler.

Re: Template Explicit Instantiation error

by Ian Lance Taylor-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

GodfatherofSoul <godfatherofsoul2004@...> writes:

> I'm trying to migrate some Visual Studio code to gcc, but I'm getting errors
> with some of our template usage:
>
> class ParentClass
> {
> public:
>   class NestedForwardDeclaration;
>
>   template class SomeTemplateClass<NestedForwardDeclaration>;
>
>   class NestedForwardDeclaration
>   {
>   ...
>   };
>
> };
>
> BTW, this is distilled down to the core cause of the problem, so you really
> can't see the rationale for this convention.  The complication error I get
> is on the "template class.." line:
>
> error: expected `<' before 'class'
>
> Does anyone understand why this fails?  Note that this same code compiles
> and runs with the Microsoft compiler.

The above test case (without the "...") fails because
SomeTemplateClass is not a template.

I'm not sure what you are trying to do in this code.  If you are
trying to do an explicit instantiation of a template, then you have to
do the instantiation outside of ParentClass.  You can't instantiate a
template inside a class; I'm not even sure what that would mean.

Ian

Re: Template Explicit Instantiation error

by Axel Freyn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
On Mon, Nov 02, 2009 at 10:20:12AM -0800, GodfatherofSoul wrote:

> I'm trying to migrate some Visual Studio code to gcc, but I'm getting errors
> with some of our template usage:
>
> class ParentClass
> {
> public:
>   class NestedForwardDeclaration;
>
>   template class SomeTemplateClass<NestedForwardDeclaration>;
>
>   class NestedForwardDeclaration
>   {
>   ...
>   };
>
> };
>
> BTW, this is distilled down to the core cause of the problem, so you really
> can't see the rationale for this convention.  The complication error I get
> is on the "template class.." line:
>
> error: expected `<' before 'class'
>
> Does anyone understand why this fails?  

Yes, I believe so;-)

> Note that this same code compiles and runs with the Microsoft compiler.

It shouldn't... According to the C++-standard, you have to write instead

template <> class SomeTemplateClass<NestedForwardDeclaration>;

Axel

Re: Template Explicit Instantiation error

by GodfatherofSoul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This has been resolved by a response from Ian Lance Taylor (thanks!).  The gcc compiler doesn't like explicit instantiations inside class declarations.  Moving the instantiations outside of the class fixed this problem.  This also builds with the Visual Studio compiler as well.  The fix is below:

class ParentClass
{
public:
  class NestedForwardDeclaration;


  class NestedForwardDeclaration
  {
  ...
  };

};

template class SomeTemplate<ParentClass::NestedForwardDeclaration>;

GodfatherofSoul wrote:
I'm trying to migrate some Visual Studio code to gcc, but I'm getting errors with some of our template usage:

class ParentClass
{
public:
  class NestedForwardDeclaration;

  template class SomeTemplateClass<NestedForwardDeclaration>;

  class NestedForwardDeclaration
  {
  ...
  };

};

BTW, this is distilled down to the core cause of the problem, so you really can't see the rationale for this convention.  The complication error I get is on the "template class.." line:

error: expected `<' before 'class'

Does anyone understand why this fails?  Note that this same code compiles and runs with the Microsoft compiler.