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

  • Home
  • SEARCH
  • 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 8854799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:58:01+00:00 2026-06-14T13:58:01+00:00

I was reading litb’s answer to a question here , where he details how

  • 0

I was reading litb’s answer to a question here, where he details how to create a specialized friend function of a class template.

I tried to create an exemplar which did just what he suggests (code at the end):

// use '<>' to specialize the function template with the class template's type
friend std::ostream& operator<< <>(std::ostream& os, const foo<T>& f)

It results in a compiler error:

error: defining explicit specialization ‘operator<< <>’ in friend declaration

Explicitly declaring the template parameter in the specialization doesn’t work either:

friend std::ostream& operator<< <T>(std::ostream& os, const foo<T>& f) // same error

On the other hand, changing from using a specialization to use a friend function template instead does work:

template<typename U>
friend std::ostream& operator<<(std::ostream& os, const foo<U>& f) // this works

So my questions are:

  • what is causing the first error?
  • how can I explicitly specialize the ostream operator for the surrounding class template specialization?

Exemplar code below:

#include <iostream>

// fwd declarations
template<typename T> struct foo;
template<typename T> std::ostream& operator<<(std::ostream&, const foo<T>&);

template<typename T>
struct foo
{
    foo(T val)
        : _val(val)
    {}

    friend std::ostream& operator<< <>(std::ostream& os, const foo<T>& f) // error line
    //template<typename U>
    //friend std::ostream& operator<<(std::ostream& os, const foo<U>& f) // this works
    {
        return os << "val=" << f._val;
    }

    T _val;
};


int main()
{
    foo<std::string> f("hello world");
    std::cout << f << std::endl;
    exit(0);
}
  • 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-14T13:58:02+00:00Added an answer on June 14, 2026 at 1:58 pm

    In litb’s example, he’s just declaring the specialization as a friend in the class. He’s not defining the specialization, which is what your code’s doing. You’re not allowed to define a specialization in a class declaration (or any non-namespace scope).

    What you need is something like:

    template <class T>
    class foo;
    
    template<class T>
    std::ostream& operator<<(std::ostream& os, const foo<T>& f)
    {
        return os << "val=" << f._val;
    }
    
    template<typename T> 
    struct foo
    {
        // ...
    private:
        friend std::ostream& operator<< <>(std::ostream& os, const foo<T>& f);
        T _val;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading litb's question about SFINAE here and I was wondering exactly what
Reading what is currently the top answer to a recent question on how/whether to
Reading some questions here on SO about conversion operators and constructors got me thinking
Reading through the C specs I found this function: double remquo(double x, double y,
Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions? ,
Reading source code of my current project, I see: [self retain] in one class,
Reading the Python 3.2 tutorial here , towards the end one of the examples
Reading the section Zend_Application_Resource_Modules in the docs here: http://framework.zend.com/manual/1.10/en/zend.application.available-resources.html I noticed this: You can
Reading this question , I wondered how much time (asymptotically speaking) does it takes
Reading this excellent answer about password hashing and wondering how to implement it: The

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.