tags:

views:

420

answers:

8

I know the names of most of the operators but not sure what operator<< and operator>> are called.

i.e.

operator=() // the assignment operator
operator==() // the equality of comparison operator
operator++() // the increment operator
operator--() // decrement operator etc.
operator<() // the less-than operator

etc eetc

+13  A: 

<< left shift

>> right shift

Mitch Wheat
Ah. So the use of the operator<< has been bastardised for the stream implementation. i.e. std::cout << "Hello Mum" << std::endl.
ScaryAardvark
@ScaryAardvark: It's called overloading
0A0D
In linguistics it is called [bastardisation](http://en.wiktionary.org/wiki/bastardisation).
dreamlax
Bastardised == overloading. Depending of course on who does the overloading ;)
Wayne Werner
@ScaryAardvark: And this is what confuses me most in C++.
Felix Kling
@Wayne: That depends on if you consider cout << degrading the C++ language :)
0A0D
@Changeling: actually... Operator is supposed to behave according to it's notation. I.e. * should multiply, / should divide, etc. And << should left shift. Instead it dumps information into stream. Which can be called bastardization.
SigTerm
@SigTerm et al: the + adds ints and concatenates strings, is that bastardization? I don't think << being "overloaded" in this way causes much confusion or buggy code... the + is probably worse from that perspective if anything.
Scott Stafford
@Felix: There is nothing in C++ that confuses you more than overloading of `operator<<`, seriously? What about the most vexing parse or the static initialization order fiasco or the countless cases of undefined behavior?
FredOverflow
@SigTerm: Are you against overloading?
0A0D
I think @Jon Purdy explains it well by saying that they should be called insertion and extraction operators (i.e. inserting bits/string). The meaning is not totally lost if you think of it in that manner, which is what it eventually evolved into IMHO. If you use * as an example, well * is used for multiplication and pointers.
0A0D
@0A0D: "Are you against overloading?" Looks like another strawman argument to me. Overloading has nothing to do with what I said.
SigTerm
@Scott Stafford: "concatenates strings," result of concatenation contains both original strings, it is their "sum", hence the +. Logical enough. "I don't think << being" I disagree and that's the end of it. Operator behavior should be consistent, and even if using "shift" operators for iostream is standard, it doesn't mean it is good. Because behavior differs, there should different operators for bitwise shifts and iostreams.
SigTerm
@0A0D: "(i.e. inserting bits/string)". Incorrect. "stream >> i" takes data from stream, puts it into variable and returns reference to same stream. "i >> 2" does not put value into "2", it shifts value of i and returns new value. I.e. we have inconsistent behavior. Which is bad, even if everyone is doing it...
SigTerm
@SigTerm: Just saying that if you are against one operator being ambiguous, then you should be against all.
0A0D
@0A0D: "Just saying that if you are against one operator being ambiguous, then you should be against all". Nope. It is the only one ambiguous operator.
SigTerm
+7  A: 
<< = Bitwise left shift
>> = Bitwise right shift
GaryDevenay
+4  A: 

Bit Shift Operators

davbryn
+16  A: 

<< is both the insertion operator and the left-shift operator.
>> is the extraction operator and the right-shift operator.

In the context of iostreams, they are considered to be stream insertion/extraction. In the context of bit-shifting, they are left-shift and right-shift.

Scott Stafford
+1 You make an important distinction. The name of the operator changes based on how it is being used (and more importantly, what type of data it is being used on). Due to this peculiarity, I have heard the `<<` and `>>` notation referred to as the "double-left" and "double-right" signs as a generic way of referencing the symbol in a context-free manner (similar to how you could call `+` "plus" or `/` "slash" no matter how they were overloaded), but this is in no way official.
bta
+1  A: 

<< is the 'left-shift' operator. It shifts its first operand left by the number of bits specified by its second operand.

Fraser
+9  A: 

In C++ Streams,

  • << is insertion operator.
  • >> is extraction operator.

In Binary Operations,

  • Right shift (>>)
  • Left shift (<<)
Dave18
+4  A: 

The original names were left shift operator (<<) and right shift operator (>>), but with their meanings perverted by streams into insertion and extraction, you could argue that even in bitwise operations << inserts bits on the right while >> extracts them. Consequently, I almost always refer to them as the insertion and extraction operators.

Jon Purdy
A: 

They are called the Guillemet Left and Guillemet Right symbols :)

David Relihan
Do you have a reference. Or did sarcasm just fly over my head?
Martin York
They *look like* Guillemet marks, but calling them such would imply that each `<<` needs a closing `>>`. I have seen some beginning programmers who notice the similarity and make the mistake of trying to use the `«` and `»` characters directly :)
bta
Good point about requiring closing >> - too smart for my own good! They're actually used in Perl too: http://en.wikipedia.org/wiki/Guillemets
David Relihan