cin >> x, just makes so much sense now.
Analogy
This thing is just syntax sugar which c++ provides.
Like when we want to write: x = x + a We can instead write: x += a
Which is exactly the same thing.
edit: these are not exactly the same things, and thus not even a good analogy, but fine to get the point.
What is operator>>
Similarly we have operator>>, which is used to take in input from an input stream. And depending on the type for the x variable two things can happen.
If x is a built-in type then cin.operator>>(x): is called, which is a member function of the cin class, and has been defined for a built-in type.
But if x is not a built-in type, then we have to manually provide how the input is going to be interpreted.
By defining a function with the below signature:
std::istream& operator>>(std::istream& is, my_type& x){};
This is a function which takes in a reference to an input stream and a reference to our variable x and defines how x is going to be filled.
and when we do: cin >> x;
For our custom type x, the above function is called: operator>>(cin,x);
And not: cin.operator>>(x);
Since there is no member function of cin defined for type of x.
That is crazy. And just makes so much sense now, >> and << are just syntactical sugar over the actual function calls.
same for operator<<
Again for outputting two things could happen depending on the type of x.
If x is a built-int type then: ostream.operator<<(x);
If x is not a built-in type then: operator<<(ostream,x);
why cin >> x >> y >> z; works?
Now the amazing thing about why we can do this: cin >> x >> y >> z; ( symmetrically the same applies to operator<< for outputting)
This is because operator>> returns an input stream and if we remove the syntactical sugar with the actual function calls being made in order, we have something like this:
cin >> x >> y >> z;
cin.operator>>(x) >> y >> z;
cin.operator>>(x).operator>>(y) >> z;
cin.operator>>(x).operator>>(y).operator>>(z);
And this makes sense because cin.operator>>(x) returns an input stream and then we get the .operator>>() member function for that returned input stream and call that with y and so on with z.
That is just so cool and everything seems to be demystified now. That is crazy. really.

PS: This is my first blog, please point out the mistakes I made.








Auto comment: topic has been updated by walrus137 (previous revision, new revision, compare).
Auto comment: topic has been updated by walrus137 (previous revision, new revision, compare).
Not
std::istream operator>>(std::istream& is, my_type& x){};butstd::istream& operator>>(std::istream& is, my_type& x){};.My bad, Thank you for pointing it out.
In fact, every operator of class type in C++ works the same. For example,
string s="Hello",t="World"; s+=t;is equivalent tostring s="Hello",t="World"; s.operator += (t);Thanks for that. This just seems amazing, that these things can be this simple and intuitive under the hood.
x += ais not the same asx = x + a.The following code fails in custom invocation with TIME_LIMIT_EXCEEDED:
The following doesn't:
That makes sense, Thanks for that.
When I wrote
x += a, I had integers in mind, but still even with integers what I wrote is wrong.I guess because when we do
x = x + a, we get a new variable whose value isx + ausingoperator =on it invokes the copy constructor and creates a copy of this already existing variablex + aand copies it tox, so we had two copies of the same thing, badBut with
x += awe addatoxitself and we don't create a copy.Note that
=inx = x + acalls the move assignment with method signaturestring& operator=(string&& str)becausex + ais an r-value. So, that does not make an additional copy.xwill free its memory and take the memory ofx + a. Only problematic part is allocation of temporary strings due to the+operation.x += ajust callsx.append(a)internally.Edit: Helpful blog though. I don't know why it was downvoted.
Oh, that is true, makes sense, So the in efficiency is due to the fact we are creating a temporary r-value string.
Thanks for pointing that out. :)
absolutely elite font choice
I know right :)
its JetBrains Mono right? i grew up as a programmer on that font with PyCharm 2020
Yup!!
Auto comment: topic has been updated by walrus137 (previous revision, new revision, compare).
#include<iostream> is also just styntactical sugar !!!
using namespace std; is also just syntactical sugar!!!
C++ is just syntactical sugar!!!
why do you call this syntactical sugar? you're just using standard operators, so there is no sugar. or do i misunderstand the meaning of this concept?
If you're familiar at all with the greatest programming language, the simplest example of syntactic sugar is the single apostrophe:
'xis syntactic sugar for(quote x). Because writing(quote sym)all the time when we want an unevaluated symbol is annoying, this syntax was introduced, even though it's unnecessary and not strictly fundamental.I'm not a C++ expert so I don't know how fundamental operator overloading is (is the AST generated by
x.operator+(y)the same as the AST generated byx+y?) but if C++ operator overloading is just an equivalent alternative to writingoperatorXYZeverywhere, and there is nothing inherently different about it aside from syntax, it is just syntactic sugar.An operator is a shorthand for calling a function/method, except for primitive types such as int, float, etc. Consider what would happen if we did not have operator overloading.
a + b - cwill look likesub(add(a, b), c). We are used to infix notation so the former is easier to read.Quoting Wikipedia:
Although, in the case of C++, syntactic diabetes is the more appropriate term. :p
If C++ was a, new, well-designed languages then the following would work:
Instead, we have to do this:
P.S. Most C++ LSPs support go to definition for operators, so you can do
Ctrl + Left clickon an operator to go to the function/method that is being called.This is operator overloading and it can be done with almost any operator