请教一个阅读STL时遇到的问题

unkstar 2004-10-08 02:03:13
最近迷上了Plauger大叔的STL库。。一边使用一边trace它的源代码,可是发现了一个问题是关于allocator中的rebind,其定义如下:

template
class allocator
: public _Allocator_base<_Ty>
{
//....other code...
template
struct rebind
{ // convert an allocator<_Ty> to an allocator <_Other>
typedef allocator<_Other> other;
};
//....other code...
};
而在使用的时候的写法如下:
typename _Alloc::template rebind<_Node>::other _Alnod;

typename是为了使用struct rebind存在,那么中间那个template呢?为什么要加那个template?从bnf分析的角度来看,似乎也无法解释它的存在。。。我用的是VS.net2003,而我把这个问题交给我的数据结构老师后,他说他也无法说明这个问题,不过他说sgi等其他的STL中没有这样的用法,但VC++.Net中到处都是。。。
请各路高手赐教。。。。。
...全文
124 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
unkstar 2004-10-09
  • 打赏
  • 举报
回复
找到原文如下。。。基本上明白了。。谢谢 fangrk(加把油,伙计!) 提供的info
9.3.3 Dependent Names of Templates
A problem very similar to the one encountered in the previous section occurs when a name of a template is dependent. In general, a C++ compiler is required to treat a < following the name of a template as the beginning of a template argument list; otherwise, it is a "less than" operator. As is the case with type names, a compiler has to assume that a dependent name does not refer to a template unless the programmer provides extra information using the keyword template:

template<typename T>
class Shell {
public:
template<int N>
class In {
public:
template<int M>
class Deep {
public:
virtual void f();
};
};
};

template<typename T, int N>
class Weird {
public:
void case1(Shell<T>::template In<N>::template Deep<N>* p) {
p->template Deep<N>::f(); // inhibit virtual call
}
void case2(Shell<T>::template In<T>::template Deep<T>& p) {
p.template Deep<N>::f(); // inhibit virtual call
}
};
This somewhat intricate example shows how all the operators that can qualify a name (::, ->, and .) may need to be followed by the keyword template. Specifically, this is the case whenever the type of the name or expression preceding the qualifying operator is dependent on a template parameter, and the name that follows the operator is a template-id (in other words, a template name followed by template arguments in angle brackets). For example, in the expression

p.template Deep<N>::f()
the type of p depends on the template parameter T. Consequently, a C++ compiler cannot look up Deep to see if it is a template, and we must explicitly indicate that Deep is the name of a template by inserting the prefix template. Without this prefix, p.Deep<N>::f() is parsed as ((p.Deep)<N)>f(). Note also that this may need to happen multiple times within a qualified name because qualifiers themselves may be qualified with a dependent qualifier. (This is illustrated by the declaration of the parameters of case1 and case2 in the previous example.)

If the keyword template is omitted in cases such as these, the opening and closing angle brackets are parsed as "less than" and "greater than" operators. However, if the keyword is not strictly needed, it is in fact not allowed at all. [6] You cannot "just sprinkle" template qualifiers throughout your code.

[6] This is actually not totally clear from the text of the standard, but the people who worked on that part of the text seem to agree.
youngphoneix 2004-10-08
  • 打赏
  • 举报
回复
template是可选的
fangrk 2004-10-08
  • 打赏
  • 举报
回复
<C++ Templates>中文版P128:
如果限定符号(::)前面的名称或者表达式依赖于某个模板参数,并且紧跟在限定符后面的是一个template-id(就是指一个后面带有尖括号内部实参列表的模板名称),那么就应该使用关键字template(原文为typename,估计写错了)。

我也不是很明白,不过记得在这本书中有这段话,就翻出来了,有空去看看。
unkstar 2004-10-08
  • 打赏
  • 举报
回复
T_T
anyone give me a hand?
难道要我给Plauger大叔本人发信?
unkstar 2004-10-08
  • 打赏
  • 举报
回复
阿。。。可能是复制的时候漏掉了。。
fangrk 2004-10-08
  • 打赏
  • 举报
回复
好像是:


template <cloass _Other>
struct rebind
{
typedef allocator<_Other> other;
};

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧