Why is it not possible to overload the ternary operator?
I think the main reason at the time that it didn't seem worth
the effort of inventing a new syntax just for that operator.
There is no token ?:
, so you'd have to create a number of
special grammar rules just for it. (The current grammar rule
has operator
followed by an operator, which is a single
token.)
As we've learned (from experience) to use operator overloading
more reasonably, it has become apparent that we really shouldn't
have allowed overloading of &&
and ||
either, for the
reasons other responses have pointed out, and probably not
operator comma as well (since the overloaded versions won't have
the sequence point which the user expects). So the motivation
to support it is even less than it was originally.
Why can't a ternary operator be overloaded?
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 overloadingexpr1?expr2:expr3
would not be able to guarantee that only one ofexpr2
andexpr3
was
executed.
How to overload the ternary operator (?:) in C++?
You can't (§13.5/3):
The following operators cannot be overloaded:
.
.*
::
?:
nor can the preprocessing symbols
#
and
##
(Clause
16).
Overload ternary ?: operator, or change to if{}else{} in included files
According to the C++ standard you are not permitted to overload ?:
The best you can do is use C macros (but this can lead to horrible code).
How to overload the conditional operator?
Several operators cannot be overloaded. These operators take a name, rather than an object, as their right operand:
Direct member access (.)
Deference pointer to class member (.*)
Scope resolution (::)
Size of (sizeof)
The conditional operator (?:) also cannot be overloaded.
Additionally, the new typecast operators: static_cast<>, dynamic_cast<>, reinterpret_cast<>, and const_cast<>, and the # and ## preprocessor tokens cannot be overloaded.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=23
Can I overload the conditional operator in C#?
The list of overloadable operators is here. Some you can, and some you can't.
This page shows how to overload the ones that can be overloaded.
As for the conditional operator (?:), no, you cannot overload it. It is specific to booleans. What would it mean if you did overload it? It's a shorthand for if/then/else.
I suppose the way you could rephrase the question is: "Why can't I overload if/then/else"?
The real reason, more than likely, probably has to do with the fact that the designers of the language chose not to implement it. More than likely, providing an overload for this operator doesn't provide enough benefit to merit the testing and effort that would go into making it overloadable. In short, it's probably a time/money reason. For every feature added to a computer language, that means time, effort and testing, which has to be balanced against the benefit provided by the feature.
In the end, some things just aren't worth implementing. While I'm sure you can find some benefit to it, the benefit is probably outweighed by the cost of implementation.
Java ternary operator function overloading
First of all, the problem doesn't have anything to do with the fact that you have overloaded versions of f
. If you only have the version of f
that takes an int
, you get the same problem.
The thing is that both possible results of the ternary expression (before and after the :
) must have the same type, because the whole expression condition ? expr1 : expr2
must have a single type. You can't have this expression evaluate to one type of condition
is true
, and another type if it is false
.
So, the Java compiler is going to see if it can convert expr1
and expr2
to a single type. Note that int
cannot be null
(because it's a primitive type). However, 10
can be converted to Integer
via autoboxing, and an Integer
can also be null
. So the type of the whole ternary expression is determined to be of type Integer
. The result is either an Integer
that contains the value 10
or an Integer
that is null
.
Step two is that you pass this Integer
to f
. Because f
takes an int
, it is auto-unboxed.
If you auto-unbox an Integer
that is null
, you get a NullPointerException
- that's what happens here.
Call Overloaded methods with ternary operator?
Method resolution is done in compile time. At the end of the day, you pass some expression that returns a value to a method. The compiler inspects the expression's type and determines which method it should call.
Here, you're attempting to write an expression that may return different types according to runtime information and invoke a method accordingly. And as you've seen, this just won't fly. Instead, you could explicitly invoke the different methods according to the type (the fact that they have the same name is inconsequential - they are still different methods!):
if (object instanceof Integer) {
Foo.load((Integer) object); // Calls Foo.load(Integer)
} else if (object instanceof String) {
Foo.load((String) object); // Calls Foo.load(String)
} else {
Foor.load(object); // Calls Foo.load(Object)
}
Related Topics
Casting to Void* and Back to Original_Data_Type*
Using Std::Variant with Recursion, Without Using Boost::Recursive_Wrapper
Piping Input into a C++ Program to Debug in Visual Studio
Can Someone Explain About Linux Library Naming
Hash Function for User Defined Class. How to Make Friends?
Getting Actual File Name (With Proper Casing) on Windows
How to Create a N Way Cartesian Product of Type Lists in C++
Emplace an Aggregate in Std::Vector
Tcp Winsock: Accept Multiple Connections/Clients
Floating Point to Binary Value(C++)
Move the String Out of a Std::Ostringstream
Adding Quotes to Argument in C++ Preprocessor
Segmentation Fault Before Main() When Using Glut, and Std::String
Why Do You Need to Append an L or F After a Value Assigned to a C++ Constant
Std::Set Iterator Automatically Const
How to Set the Background Image in Qt Stylesheet