开发者

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

开发者 https://www.devze.com 2022-12-22 07:33 出处:网络
The following fails to compile (with gcc 4.2.1 on Linux, anyway): template< typename T > class Foo

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 )开发者_StackOverflow
{
   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().


use typename:

  typename Foo< T >::FooType bar = 0;

See this for why typename is needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消