Why can't a ternary operator be overloaded? [duplicate] - c++

This question already has answers here:
Why is it not possible to overload the ternary operator?
(6 answers)
Closed 5 years ago.
I was always curious about that, but never found a solid answer or an easy to understand explanation. I have tried with other operators and it works just fine. This operator in particular is an interesting one. I just can't get my head wrap around it.

FAQ of stroustrup :
There is no fundamental reason to disallow overloading of ?:. I just
didn't see the need to introduce the special case of overloading a
ternary operator. Note that a function overloading expr1?expr2:expr3
would not be able to guarantee that only one of expr2 and expr3 was
executed.

Related

is overloading parantheses c++ a good idea [duplicate]

This question already has answers here:
What are C++ functors and their uses?
(14 answers)
Closed 4 years ago.
I have programmed in c++ for a couple of months, and I am starting to understand the core. At the moment I am trying to make a calender program with classes and inheritance (just to get more comfortable with object oriented programming), and a few weeks ago I learned about operation overloading.
I am wondering, is it a bad idea to overload parantheses for an object, such that I could for instance write this, or can an error occure becuase the compiler can confuse it for something else(constructor or something like that)?
//creating a valid year-object
Year year1998 = Year(1998,true);
// the parantheses operator returns a day(another object)
Day d = year1998(1,10);
//the overloading
Day& Year::operator()(int monthNumber, int dayNumber){
//Just returns a day from the month class
return months[monthNumber][dayNumber];
}
Overloading this operator is a basic concept in C++ and is extensively used in function objects, a typical example being comparison functions.
You can savely use this operator, but nowadays often lambda functions are preferred to function objects.

In c++ why we can overload `operator ->` and can't overload `operator .` [duplicate]

This question already has answers here:
Why can't you overload the '.' operator in C++?
(4 answers)
Closed 5 years ago.
In c++, why we can overload operator -> and can't overload operator .?
Similarly, why we can overload operator ->* and can't overload operator .*?
I will be appreciated if you can help me!
Stroustrup actually has this as an FAQ question on his website. To quote his answer:
Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by .
This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best
(Note that Stroustup himself has since proposed allowing overloading of operator., as have others, so we may see it in a future version of the language.)

Overload the assignment operator in C++ [duplicate]

This question already has answers here:
C++ assignment operator - compiler generated or custom?
(5 answers)
Closed 7 years ago.
I have a general question i C++ that I coudln't get a clear answer to and I'm hoping I get an answer to it here. As a programmer, do I need to explicitly overload the assignment operator for a class when all instance data fields in this class are standard built in C++ data types.
No. You only need to overload the defaults if you are doing manual resource management. If all of your members can be treated as POD types then you will be fine.
No, you don't.​​​​​​​​​​​​​​​​​​​​​

How to overload -> operator in C++ [duplicate]

This question already has answers here:
Overload -> operator to forward member-access through Proxy
(3 answers)
Overloading member access operators ->, .*
(5 answers)
Closed 8 years ago.
As per the title, how to overload -> operator in C++?
I can't find any documentation.
cppreference glosses over it.
The Wikipedia page on overloading similarly glosses over it.
Operator overloading <-- this SO post again has a '->' shaped hole in it, although one comment gives a hint:
operator->() is actually extremely weird. It's not required to return
a value_type* -- in fact, it can return another class type, provided
that class type has an operator->(), which will then be called
subsequently. This recursive calling of operator->()s proceeds until a
value_type* return type occurs. Madness! :)
It appears that this particular operator is not straightforward to overload.
Could someone link to documentation, or (preferably) provide some here?
PS I recognisze this is an unusual overload, I have need of it in a Proxy pattern, here

finding addressof a object whose ampersand operator is overloaded and private [duplicate]

This question already has answers here:
If an operator is overloaded for a C++ class how could I use a default operator instead?
(3 answers)
Closed 8 years ago.
I have a class which is overloading ampersand(&) operator and made it private. I don't have a latest C++11 compliant compiler so is there any way by which I can get address of my object using current C++ compiler only
Thanks
reinterpret_cast<T *>(&reinterpret_cast<char&>(obj))
Dunno if it's safe though.. (well clearly it's a bit dodgy)
Use the addressof() function from Boost.Utility. If you don't want to use Boost, you can still look at it's implementation which consists of just a single header.