x86-Debug x86-Debug - 4 months ago 10
C++ Question

inner class method definition outside of a template class

I have class

that defines inner class

template <class T, int SIZE = 100>
class Array
// my class here
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
? Is this standard compliant? What if there is another
class outside of class


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).

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:

// 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
{ ... }