-6

Consider the context, where i want to write to another file descriptor than stdout. Here, My "write" method prints to stdout, but only for understanding.

THIS IS THE MAIN PROBLEM I AM TRYING TO SOLVE:

I have a write(char*) method, i want to convert to c++ style stream through operator<<, to be able to write in C++, obfuscating multiple explicit calls of write, allowing to write in a single line of code without having to concatenate or any other solution.

PERIOD.

Though, on internet, there is nowhere to find an example that overloads operator<< with anything else than ostream on left operand. does that mean, my following example (which compiles! i tested it on a codepen-like website) is a rare case of working overload without ostream, or did i misconcepted the whole thing and "Ter" should be an ostream by inheritance ?

EDIT: How can this be a duplicate ? I am legitimately intrigued why both the topic you linked, and https://arne-mertz.de/2015/01/operator-overloading-common-practice/ or many others never talk about a left operand from another type than ostream.

EDIT2: I am having trouble to be precise on my question. Is the following code a good practice, if not, what should i do to solve the problem i braced with capital letters. Thank you for your answers

#include <iostream>

struct Ter {
    void write(const std::string &str) { std::cout << str; }; //Here we should do something else in our context
    friend Ter &operator<<(Ter &a, std::string &b);
};


Ter &operator<<(Ter &a, const std::string &b)
{
   a.write(b);
   return (a);
}


int main()
{
    Ter toto;
    toto << "b" << "c";
}
stalker2106
  • 118
  • 9
  • 1
    It depends on what you want `operator<<` to mean for `Ter`. In this case, it sure looks like it wants to be an `std::ostream`. – François Andrieux Oct 10 '18 at 17:55
  • 1
    Typically, when you overload an operator, you should do what it's built in counterpart does. In C++ `>>` and `<<` is either bitwise operation or streaming. If you want to concatenate something then `+` and `+=` are used. – NathanOliver Oct 10 '18 at 17:56
  • Why should one use anything else than the interface? – πάντα ῥεῖ Oct 10 '18 at 17:57
  • Thank you NathanOlivier, this is right. I understand for the concatenation operators, however, concatenation is a WAY of solving the problem, and not the solution we are searching for. We are actually looking to know why nobody replace multiple calls of a method with a stream operator that would call it in a single manner – stalker2106 Oct 10 '18 at 18:01
  • @NathanOliver Reopened. But well the main principles of operator overloading are at least related. – πάντα ῥεῖ Oct 10 '18 at 18:03
  • @πάντα ῥεῖ Indeed they are, but the example of "operator<<" is still written with an ostream as left operand. I dare you to find somewhere on the internet where an exemple of overload of operator<< exists with a left operand of T type that is not ostream& – stalker2106 Oct 10 '18 at 18:04
  • @stalker2106 Concatenation might be better expressed using an overload of `operator+()`, though `std::string` concatenation might be easily achieved as a combination of overloading `operator<<()` and using `std::ostringstream`. – πάντα ῥεῖ Oct 10 '18 at 18:06
  • @πάντα ῥεῖ I don't want to concatenate, but call write two times internally with the strings i supplied in the stream operation. Am I right ? – stalker2106 Oct 10 '18 at 18:09
  • 4
    @stalker2106: "*I dare you to find somewhere on the internet where an exemple of overload of operator<< exists with a left operand of T type that is not ostream&*" [Boost.Spirit](http://boost-spirit.com/home/). Also [Boost.Serialization](https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/index.html); archives [are not streams](https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/rationale.html#archives). Also Boost's dynamic bitset type. – Nicol Bolas Oct 10 '18 at 18:10
  • @Nicol Bolas On every link you supplied, when i am looking for "<<" in my browser search, nothing shows! Where can I find an example of an overload on these pages ? – stalker2106 Oct 10 '18 at 18:15
  • 1
    It's difficult to read this as a clear, specific question. _"Why do people always..."_ They don't always. _"There is nowhere to find..."_ There is. _"Does that mean my example is a rare case..."_ No, it doesn't mean that. – Drew Dormann Oct 10 '18 at 18:16
  • 1
    @stalker2106: I didn't link you to code; I linked you to *libraries*. Feel free to find the code within those libraries yourself. – Nicol Bolas Oct 10 '18 at 18:16
  • Can you clarify what kind of answer you're looking for? The new question in your title is answered in the question itself. _You_ provide an example. Are you looking for links to popular libraries? Or are you asking if your code is wrong? – Drew Dormann Oct 10 '18 at 18:23
  • @Drew Dormann I am actually CTRL+F-ing Boost sources as suggested by Nicol Bolas an I managed to find examples which is nice and partially answers my question telling me this is 'valid'. I also edited the question to be more precise. – stalker2106 Oct 10 '18 at 18:31
  • your question is rather vague. If you have working code and you are not sure if it is a good way to write it you may have more luck on https://codereview.stackexchange.com – 463035818_is_not_an_ai Oct 10 '18 at 18:39

1 Answers1

1

I am legitimately intrigued why both the topic you linked, and https://arne-mertz.de/2015/01/operator-overloading-common-practice/ or many others never talk about a left operand from another type than ostream.

Because it does not really matter. What works for std::ostream works similar for other types. Sorry, but its a bit like getting an example of overloading operator+ for a type Foo and then complaining that you never get an example of how to overload it for a Bar. If you want your type to offer a operator<< then yes, you implemented it in way that works.

Your code seems to be correct. Whether it does what you want, only you can decide.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • This is a good answer thank you. Yeah well, I'm just intrigued why (for example in the link i supplied) all the operators are overloaded for a T or X type, and operator<< is always with an ostream. and i find your answer very relevant because you show a use case, and with + operator anybody understands x + y, but people wont for a << b. thank you. – stalker2106 Oct 10 '18 at 18:48
  • @stalker Thats because typically you do care about the right hand side and do want to supply an overload for `std::ostream`s `operator<<` – 463035818_is_not_an_ai Oct 10 '18 at 19:04