Defining an inner class method outside the template class

I have a class Arraythat defines an inner classconst_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 the class I defined a function and used it const_iterator positionas the first type of argument instead of writing typename Array<T, SIZE>::const_iterator position? Is this standard? What if there is another class const_iteratoroutside the class Array?

+4
source share
1 answer

Yes, that’s fine and standard (well, in the class you need to declare const_iteratorfirst before your member function insert()takes a parameter of this type).

- , , . , const_iterator Array, , , . , , :

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

Source: https://habr.com/ru/post/1650168/


All Articles