Robin Chow's C++ Blog

 

[导入]More Exceptional C++ 之 4

typename 常常在模板类的声明或定义中用来声明一个类型。在模板中,如果一个和模板参数相关的名字没有在名字解析中被找到,或者被关键字 typename 修饰,则它不能作为一个类型。考虑下面的代码:
template< typename T >
class X_base
{
public:
typedef T instantiated_type;
};

template< typename A, typename B >
class X : public X_base< B >
{
public:
bool operator()( const instantiated_type& i ) const
{
return i != instantiated_type();
}
// ... more stuff ...
};

instantiated_type 和模板参数类型相关,使用名称解析不能找到并且没有被关键字 typename 修饰,因此将导致编译错误,正确的方法是使用关键字 typename。
文章来源:http://my.donews.com/robinchow/2007/01/18/gywtqxaalrvetoldhdtlmchhlsmnfxklwgem/

posted on 2007-10-23 20:47 Robin Chow 阅读(73) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜