How to declare a friend of a nested class of a template class?

When I do the following:

template <typename T>
class Container
{
public:
    class Iterator
    {
        friend bool operator==(const Iterator& x, const Iterator& y);
    };
};

gcc gives me the following warning and suggestion:

warning: friend declaration 
'bool operator==(const Container<T>::Iterator&, 
                 const Container<T>::Iterator&)' 
declares a non-template function [-Wnon-template-friend]

friend bool operator==(const Iterator& x, const Iterator& y);
                                                           ^

(if this is not what you intended, 
 make sure the function template has already been declared 
 and add <> after the function name here)

I am sure this is a new warning, as I always did it and never experienced any problems.

Can someone explain why this is a warning and what it is warning?

+4
source share
2 answers

This is a warning that it is almost impossible to determine what operator==is outside the class.

That is, the ad friendsupports the non-template function operator==- for example, it Container<Int>::Iteratorhas a function as a friend

bool operator==(const Container<Int>::Iterator&, const Container<Int>::Iterator&);

, operator== Container .

template<class T>
bool operator==(const Container<T>::Iterator&, const Container<T>::Iterator&);

. ( , , T .)

- , . ( Iterator , T.) - .

+6

friend bool operator==(const Iterator& x, const Iterator& y);

, const typename Container<T>::Iterator&. , , Container T, operator==.

operator== , . , operator==, , - , Container. .

, , , .

, , , :

template <typename T>
class Container_Iterator;

template <typename T> bool operator==(const Container_Iterator<T>& x,
                                      const Container_Iterator<T>& y);

template <typename T>
class Container
{
public:
    typedef Container_Iterator<T> Iterator;
};

template <typename T>
class Container_Iterator {
  friend bool operator==<T>(const Container_Iterator<T>& x,
                            const Container_Iterator<T>& y);
};

// btw, don't forget to define operator==

operator== , operator== .

+4

All Articles