Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Advanced
Jun 23, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Declare a Function Template as a Friend of Another Class Template

There are various forms of friend declarations within a class template: a non-template friend, a specialized template friend, and a class template friend. A function template can also be declared as a friend of a class template. For instance, you might add an overloaded operator == function template to test the equality of two Vector objects. By doing so, you ensure that for every specialization of Vector, the implementation will generate a corresponding specialization of the overloaded operator == as well. Unfortunately, the declaration of a function template friend is rather complex. This following example walks you through these intricate steps and demonstrates how to do the declaration.

In order to declare a template function as a friend, you first have to forward declare the class template and the friend function template as follows:

  template <class T> class Vector; // class template forward declaration
    // forward declaration of the function template to be used as a friend
  template <class T> bool operator== (const Vector<T>& v1, const Vector<T>& v2);

Next, you declare the friend function template inside the class body:

  template <class T> class Vector
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date