21

Looking into some code of a colleague of mine, I came accross the following:

friend bool operator==<>(ValueIter<Type> const &rhs, ValueIter<Type> const &lhs);

It is declared in a template class:

template<typename Type>
class ValueIter: public std::iterator<std::bidirectional_iterator_tag, Type>

Can someone tell me what the ==<> symbol indicates? I expect it has something to with the != operator.

3 Answers3

32

It looks like two, the operator== that is a full template instantiation or specialisation <>.

I've seen only a few like this in the wild though.

Given the friend, the class is probably befriending the template operator.


If you are getting linker errors with it, see this answer for why.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • 13
    Reminds me of [this question](http://stackoverflow.com/q/1642028/3747990) on operator `-->`. – Niall Aug 06 '15 at 13:11
  • 3
    It indeed is a template for the operator==. I have seen this before template classes. When one removes the <> you will get an error indicating that the function is a non-template function. – Michiel uit het Broek Aug 06 '15 at 13:20
6

Your question is incomplete.

Presumably, in some context within the code you are examining, there is a templated operator==() function.

Then within some class, a particular specialisation of that templated operator==() is being declared as a friend.

Without context that you haven't given (i.e. of the preceding template definition, or of the enclosing class definition) it is not possible to give a more specific answer. There are too many possibilities for what the template or relevant specialisations are.

Peter
  • 35,646
  • 4
  • 32
  • 74
5

With

template <typename T> class ValueIter;

template <typename T> 
bool operator==(ValueIter<T> const &rhs, ValueIter<T> const &lhs);

Inside template <typename T> class ValueIter

  • friend bool operator==(ValueIter const &rhs, ValueIter const &lhs);
    and friend bool operator==(ValueIter<T> const &rhs, ValueIter<T> const &lhs);
    add friendship to a non template operator.

  • friend bool operator==<>(ValueIter const &rhs, ValueIter const &lhs);,
    friend bool operator==<>(ValueIter<T> const &rhs, ValueIter<T> const friend bool operator==<T>(ValueIter const &rhs, ValueIter const &lhs);,
    friend bool operator==<T>(ValueIter<T> const &rhs, ValueIter<T> const
    add friendship to the template operator (just for the type with match T)

  • template <typename U> friend bool operator==(ValueIter<U> const &rhs, ValueIter<U> const &lhs); add friendship to the template operator (for any type U (which may differ of T))

==<> is used in the second point and is really == <>.

Jarod42
  • 203,559
  • 14
  • 181
  • 302