【发布时间】:2015-12-14 10:51:20
【问题描述】:
下面是 CRTP 定义自定义集合类型的基本用法:
template <class __B>
struct A
{
typedef std::vector<__B> collection_type;
};
struct B: public A<B>
{
collection_type X;
};
在模板中使用
template <typename __T>
struct C: public A<C<__T>>
{
// collection_type X; <--- this does not compile
typename A<C<__T>>::collection_type X;
};
为什么 C 需要 "typename ...::" 部分,而 B 不需要?
【问题讨论】:
-
不合格的查找不查找依赖的基类。并停止命名
__T和__B。 -
停止命名事物
__T和__B。请参阅 17.6.4.3.2 全局名称 [global.names] >每个包含双下划线 _ 或以下划线后跟大写字母 (2.12) 的名称都保留给实现以供任何使用。
标签: c++ templates inheritance types crtp