Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is + operator overloaded for primitive types?

Already overloaded operators <<,>>,= etc are used many times.

An example that i was thinking of is when we add strings say :

string name = string("munish") + "kumar";

the + operator is overloaded in the string class.

but when we add numbers like 1 + 2( doesn't seem like an overloaded operator call)

I was just wondering how does it happens does the compiler does a binary additon.

I don't need to worry about it much though if the compiler does it so, just a matter of curiosity for me.

like image 850
munish Avatar asked Jun 02 '11 06:06

munish


3 Answers

Primitive types don't implement operator+ which is actually a function with a weird name. Addition for Primitive type is carried out by CPU instruction such as :

addl %edx,(%eax) //this adds two integral values loaded at edx and eax

You implement operator+ for user-defined types, and compiler generates lots of CPU instructions to carry out the task which you write in operator+.

like image 168
Nawaz Avatar answered Nov 19 '22 23:11

Nawaz


I think you're confusing two things: operator overloading, and how you define an operator for a user defined type. Operator overloading isn't new; it exists already in C, where + is overloaded on int, unsigned int, long, unsigned long, float, double and long double. In C++, you can add operators for user defined types, by defining a function named operator+, so the list of overloads is longer. And to make things more coherent, the way the compiler resolves the overload is to add synthesized signatures along the lines of operator+(int, int) to the list of overloads. If the overload resolution then ends up choosing one of these synthesized signatures, it uses the built-in operator, generating whatever machine code is necessary (which may involve a function call at the machine code level). The fact that it chooses one of the "build-in" overloads does have repercussions in the rest of the code: a user defined operator is considered a function call, with the corresponding sequence points and all of the rest, and the compiler is not allowed to make any assumptions concerning its semantics—in particular, the compiler cannot assume that a user defined operator+ is associative or commutative. A build-in operator (with a very few exceptions) does not introduce any sequence points, and the compiler knows its semantics, and can optimize in consequence.

Note that the generated machine code may not be that different. I've used machines where the compiler had to call a function to multiply ints, and the compiler can (and often will) inline a user defined operator. What changes is the rules I mentionned above: the presence of sequence points in user defined operators, and the fact that the compiler cannot make assumptions concerning the various mathematical laws which might apply.

like image 22
James Kanze Avatar answered Nov 20 '22 00:11

James Kanze


You can overload the operators on custom data types i.e classes.. int is a primitive data type and the operators on primitive types are implemented by the compiler itself, basically it is just a simple CPU instruction to add int etc

like image 1
Ankur Avatar answered Nov 19 '22 22:11

Ankur