Using typedefs from a template class in a template (non-member) function

Posted by atomicpirate on Stack Overflow See other posts from Stack Overflow or by atomicpirate
Published on 2010-03-10T16:34:25Z Indexed on 2010/04/27 11:13 UTC
Read the original article Hit count: 337

The following fails to compile (with gcc 4.2.1 on Linux, anyway):

template< typename T >
class Foo
{
public:
   typedef int FooType;
};

void
ordinary()
{
   Foo< int >::FooType bar = 0;
}

template< typename T >
void
templated()
{
   Foo< T >::FooType bar = T( 0 );
}

int main( int argc, char **argv )
{
   return 0;
}

The problem is with this line:

   Foo< T >::FooType bar = 0;

...and the compiler makes this complaint:

foo.c: In function ‘void templated()’:

foo.c:22: error: expected `;' before ‘bar’

Normally one sees this when a type hasn't been declared, but as far as I can tell, Foo< T >::FooType should be perfectly valid inside templated().

© Stack Overflow or respective owner

Related posts about templates

Related posts about typedef