开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜