8

I try to overload operator<< for my class so that it print member when I do std::cout << obj;

I see that the way to do this is

std::ostream& operator<<(std::ostream& os, const T& obj)
{
  // write obj to stream

  return os;
}

What are the basic rules and idioms for operator overloading?

However, I try to make my code conforms to Google C++ style guide https://google.github.io/styleguide/cppguide.html#Reference_Arguments

It says that passing the reference without const is not allowed except for the case that it is needed by convention such as swap(). Is this overloading operator<< in the same category as swap()? or there is a way to do something like

std::ostream& operator<<(std::ostream* os, const T& obj)
                                     ^

? or something that does not take non-const reference as the input.

If so, please teach me how to do that. Thank you.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
EKP
  • 95
  • 4
  • 8
    That seems like a pretty poor rule if you ask me. One red flag is that it has exceptions like for `swap` and copy constructors. Edit : Every time the Google Style Guide comes up I learn something new about it which leads me to lose a little bit more respect for it. Maybe it works well for them but I would not recommend it. – François Andrieux Oct 10 '18 at 15:13
  • 14
    The google C++ guidelines are widely regarded as A Bad Thing. –  Oct 10 '18 at 15:14
  • Seconded. It is the standard convention to pass the stream object by non-const reference. – NathanOliver Oct 10 '18 at 15:14
  • I highly doubt that google wants you to pass the `ostream` as pointer. Passing a non-const `&` **is** the right thing to do because it is a case where passing without const is needed (not by convention but for correctness) – 463035818_is_not_an_ai Oct 10 '18 at 15:14
  • 4
    The stream parameter must be a non-const reference, or it won't work. – molbdnilo Oct 10 '18 at 15:15
  • 1
    consider that the google guidelines are made for the google code base which probably has millions loc of legacy including worst habits and they have to try their best to keep it managable, if you write your own code then their rules can be completely off – 463035818_is_not_an_ai Oct 10 '18 at 15:15
  • Flagging this as Opinion Based. I think the question can be summed up as what does the SO community think of the Google guidelines. – Richard Critten Oct 10 '18 at 15:17
  • anyhow, if you read the google guidelines carefully, they also write "In C++, the function can alternatively declare a reference parameter: int foo(int &val)." so they arent really suggesting you to stay away from using references – 463035818_is_not_an_ai Oct 10 '18 at 15:18
  • No the Google style guide is wrong here; the code wouldn't work with a `const` reference. – Arnav Borborah Oct 10 '18 at 15:20
  • 1
    `passing the reference without const is not allowed except for the case that it is needed by convention such as swap()` - that is the answer in my opinion. In your case it is needed by convention I'd say. – Ely Oct 10 '18 at 15:23
  • The problem with many/most style guides is that they aren't clear about when they're telling you to do something because it is more correct, and when it's because they assume other engineers will respond better when they read your code. – Tim Randall Oct 10 '18 at 15:26
  • This rule actually goes against goal #2 of that guide: "Optimize for the reader, not the writer". With passing pointers around it is not clear whether we transfer ownership or not, whether parameter can be null or not. – user7860670 Oct 10 '18 at 15:27
  • Looks like the people who wrote the google convention don't understand const correctness. – UKMonkey Oct 10 '18 at 15:43
  • I've seen this rule in other places, the rational is that it is clear from the call whether or not a parameter is being mutated, rather than a reader of code needing to examine the function signature to see whether or not something being passed is non-const. I think it's kind of supposed to cater to people who don't use modern IDEs or similar features.. – John Cramerus Oct 10 '18 at 22:53

1 Answers1

7

It says that passing the reference without const is not allowed except for the case that it is needed by convention

Well, the stream is conventionally passed as a non-const reference into the stream insertion and extraction operators, so it would appear that the rule has allowed an exception for you. As such, defining the suggested overload should be conforming to the rule despite accepting a non-const reference argument.

That said, I'm not an authority on what Google would consider as convention. If you work for Google, you should know whom to ask; If you don't then you don't need to get stressed over their style.

eerorika
  • 232,697
  • 12
  • 197
  • 326