[英]Passing an iterator to a function
Looking through the source code of a binary tree,I find the following function: 查看二叉树的源代码,我发现以下功能:
//definition of BTR,in case you'd want to know
template< class Type>
struct BTR
{
// The item saved to that specifiec position into the tree
Type value;
// Points to the left leaf
BTR<Type>* left;
// Points to the right leaf
BTR<Type>* right;
};
//why typename?
template< class Type>
BTR<Type>* CreateEx(typename const std::vector<Type>::iterator start,typename const std::vector<Type>::iterator end)
{
//definition
}
Now,what's confusing me about this function, is its parameters. 现在,让我感到困惑的是它的参数。 Why does it need the keyword typename?
为什么需要关键字typename? Because if I remove both the typenames,my compiler starts complaining and says I should put a ')' before identifier 'start'.
因为如果同时删除两个类型名,我的编译器就会开始抱怨并说我应该在标识符“ start”之前加上“)”。 And if I changed the parameters so that the function took two vectors instead of two iterators and removed the typenames,my compiler stops complaining(although of course,the function doesn't work any more).
而且,如果我更改了参数,以便该函数使用两个向量而不是两个迭代器并删除了类型名,则我的编译器将停止抱怨(尽管该函数不再起作用)。
// perfectly acceptable!
template< class Type>
BTR<Type>* CreateEx( const std::vector<Type> start, const std::vector<Type> end)
So it seems I need the keyword because the function wants two iterators. 看来我需要关键字,因为该函数需要两个迭代器。 But why is this keyword necessary in a situation like this?
但是为什么在这种情况下需要这个关键字?
因为编译器不知道std :: vector <Type> :: iterator是std :: vector <Type>的类型还是成员,因此需要以typename的形式提供一些帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.