Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:52:54+00:00 2026-06-08T14:52:54+00:00

What is the correct way to declare a friend function of a template class

  • 0

What is the correct way to declare a friend function of a template class (for the std::ostream& operator<<) in a .cpp file ?

My current implementation does not work :

// MyTest.h
template<class T, unsigned int TSIZE> class MyTest
{
    inline friend std::ostream& operator<< <T, TSIZE> (std::ostream &lhs, const MyTest<T, TSIZE> &rhs);
};

// MyTest.cpp
template<class T, unsigned int TSIZE> inline friend std::ostream& operator<< <T, TSIZE> (std::ostream &lhs, const MyTest<T, TSIZE> &rhs)
{
    // IMPLEMENTATION
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:52:56+00:00Added an answer on June 8, 2026 at 2:52 pm

    To refer to operator<< <T, TSIZE> like you do, which is a template specialization, a declaration of the primary template must be visible. In turn operator<< needs a declaration of MyTest because it appears as a parameter.

    // Declare MyTest because operator<< needs it
    template<class T, unsigned int TSIZE> class MyTest;
    
    // Declare primary template
    template<class T, unsigned int TSIZE>
    inline std::ostream& operator<<(std::ostream& lhs, const MyText<T, TSIZE>& rhs);
    
    template<class T, unsigned int TSIZE> class MyTest
    {
        // Specialization MyTest<T, TSIZE> only declares
        // specialization operator<< <T, TSIZE> as friend
        // Note that you can just use '<>' to designate the specialization,
        // template parameters are deduced from the argument list in this case
        inline friend std::ostream& operator<< <> (std::ostream &lhs, const MyTest<T, TSIZE> &rhs);
    };
    

    The definition you have should match those declarations. Note that since operator<< is a template its definition should in all likeliness be in the header.

    An alternative that requires less work when it comes to writing all those preemptive declarations is for MyTest<T, TSIZE> to declare the whole template as a friend, not just the specialization that takes MyTest<T, TSIZE>.

    // in MyTest definition
    template<typename U, unsigned USIZE>
    inline friend std::ostream& operator<<(std::ostream& lhs, const MyTest<U, USIZE>& rhs);
    

    The definition you have should also match such a declaration (the name of the template parameters has no bearing on matching declarations and definition).

    For the sake of completeness, I will mention that when it comes to the friend of a class template an alternative is to define it in the class template definition. This defines a non-template friend function that is unique for each specialization.

    // in MyTest definition
    friend std::ostream& operator<<(std::ostream& lhs, MyTest const& rhs)
    { /* implementation */ }
    

    It is impossible to refer to such functions (e.g. &ns::operator<< doesn’t work, unlike the other options) and they are only found via ADL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
Why was C++ designed such that the correct way to declare two int *s
What's the correct way to set a flash message for the current view but
What would be the correct way to create a fully URL-encoded file:// URI from
What is the correct way to declare a multidimensional array and assign values to
I understand that the correct way to declare a return type const is to
Is this the correct way to declare and iterate through an array in Python,
How would you correct way to handle an exception in this situation? Initially I
What is the correct way to add a product variant? I create the product
Which is correct way to extend/subclass components? Ext.define('Holidays.Components.UserInfo', { extend: 'Ext.panel.Panel', alias: 'widget.UserInfo', region:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.