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 6738147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:19:57+00:00 2026-05-26T11:19:57+00:00

Having to declare global friend operator overloading to do serialization always struck me as

  • 0

Having to declare “global friend operator overloading” to do serialization always struck me as kludgey. It didn’t seem foundational to have to declare serialization operators outside of your class. So I was looking for a solid answer for why.

(Note: If anyone has better Google-Fu to find a good answer already written, I’d be interested to read that.)

What I suspect is that it’s technically possible, and is only a notational issue. Had the library been designed to do member overloads of << and >>, you’d have to build a line of streaming operations from right to left, instead of left to right. So instead of writing:

Rational r (1, 2);
cout << "Your rational number is " << r;

You would have to write the output line as:

r >> ("Your rational number is " >> cout);

The parentheses are needed to kick off the backwards chaining, because >> and << associate left-to-right. Without them, it will try to to find a match for r >> "Your rational number is " before "Your rational number is " >> cout. Had an operator with right-to-left associativity been picked, this could be avoided:

r >>= "Your rational number is " >>= cout;

(Note: Inside the library, non-class types like the string literal would have to be taken care of with global operator overloads.)

But is that the limit, and this reversal is pretty much inevitable for any iostream-style design that wanted serialization dispatched into the class? Am I missing any other problems?


UPDATE Perhaps a better phrasing of the “issue” is to say that I came to suspect the following:

For non-stream objects that wish to serialize themselves, the iostream library COULD hypothetically have been designed so that inserters and extracters were class members instead of global overloads…and without (significantly) affecting the runtime properties. Yet this would have ONLY worked if the iostream authors were willing to accept it would force clients to form streaming operations right-to-left.

But I lack an intuition about why global overloading an operator vs. a member could unlock an otherwise-unlockable ability to express oneself left-to-right (instead of right-to-left). Question is whether hindsight, or templates, or some esoteric feature of C++11 could offer an alternative. OR does “C++ physics” have an inherent bias for one direction over another, and global overloading is somehow the only compile-time trick in the book for overriding it.

Compare with Fleming’s left hand rule for motors

  • 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-05-26T11:19:58+00:00Added an answer on May 26, 2026 at 11:19 am

    You get more flexibility by separating the definition of << and >> from the object you’re displaying.

    First of all, you might want to custom-display a non-class type, like an enum, or a pointer. Suppose you have a class Foo and you want to custom-print a pointer to Foo. You can’t do it by adding a member function to Foo. Or you might want to display a templated object but only for a particular template parameter. For instance, you might want to display a vector<int> as a comma separated list, but a vector<string> as a column of string.

    Another reason might be that you’re not allowed or willing to modify an existing class. It’s a good example of the open/closed principle in OO design, where you want your classes to be open for extension but closed for modification. Of course, your class has to expose some of its implementation without breaking encapsulation, but that’s often the case (vectors expose their elements, complex exposes Re and Im, string exposes c_str, etc.).

    You may even define two different overloads of << for the same class in different modules, if it makes sense.

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

Sidebar

Related Questions

I'm having troubles extending the Application class to declare global variables. I have extended
I'm having trouble with global variables in php. I have a $screen var set
I'm having difficulty using MooseX::Declare properly when calling BUILDARGS. I'm trying to create an
I'm having trouble understanding why I would use a context.xml file to declare a
I was having troubles earlier while trying to declare a ChangeAction parameter in a
I have been having a hard time understanding how to use a singleton to
I'm having trouble with global variables in JavaScript. From every article I've read a
I'm having trouble trying to declare a shared memory array within the kernel. Here's
in C# you have to declare everything in a class so an example factory
I am having small problem in making a global variable works. I am using

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.