[英]inner class method definition outside of a template class
I have class Array
that defines inner class const_iterator
我有类
Array
来定义内部类const_iterator
template <class T, int SIZE = 100>
class Array
{
// my class here
public:
class const_iterator
{
// my class here
};
void insert(const_iterator position, int value);
};
template <class T, int SIZE /*= 100*/>
void Array<T, SIZE>::insert(const_iterator position, int value)
{
// impl
}
Is it normal, that outside of the class I have defined the function and have used const_iterator position
as the first argument type instead of writing typename Array<T, SIZE>::const_iterator position
? 这是正常的,在类之外我定义了函数并使用
const_iterator position
作为第一个参数类型而不是写入typename Array<T, SIZE>::const_iterator position
? Is this standard compliant? 这个标准是否合规? What if there is another
const_iterator
class outside of class Array
? 如果在类
Array
之外还有另一个const_iterator
类怎么办?
Yes, it's perfectly fine and standard (well, within the class you need to declare const_iterator
first before having your insert()
member function take a parameter of that type). 是的,这是完全正常和标准(当然,在类中你需要声明
const_iterator
有你之前首先 insert()
成员函数采取该类型的参数)。
For member function definitions outside of the class, every name that comes after the class name scope introducer is looked up within the scope of that class first. 对于类外部的成员函数定义,首先在该类的范围内查找类名范围介绍者之后的每个名称。 So if there were another
const_iterator
outside of Array
, we would still find the inner one first since that's the scope we start looking in. Note that it's only those names after the class introducer that have this special lookup: 因此,如果在
Array
之外还有另一个const_iterator
,我们仍然会首先找到内部的那个,因为那是我们开始查看的范围。请注意,只有类导入器之后的那些名称才具有此特殊查找:
// this is okay
template <class T, int SIZE>
typename Array<T, SIZE>::const_iterator Array<T, SIZE>::some_method() { ... }
// error: this does *not* find the nested const_iterator class
template <class T, int SIZE>
const_iterator Array<T, SIZE>::some_method() { ... }
// this is okay. trailing return type comes after the class introducer
template <class T, int SIZE>
auto Array<T, SIZE>::some_method()
-> const_iterator
{ ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.