Ternary operator and function signature

3k Views Asked by At

Let's say I have a C++ class with two functions like

class MyClass
{
    bool Foo(int val);
    bool Foo(string val);
}

Is it possible to use the ternary operator like this

MyClassInstance->Foo(booleanValue?24:"a string");

and have a different function of MyClass invoked depending on the value of booleanValue?

4

There are 4 best solutions below

0
On BEST ANSWER

Not with the ternary operator. The type of a ternary expression is the common type of its second and third operands; if they have no common type you can't use it. So just use an ordinary if statement:

if (booleanValue)
    MyClassInstance->Foo(24);
else
    MyClassInstance->Foo("a string");
3
On

No. To perform overload resolution the compiler will ask "what is the type of booleanValue?24:"a string"?". That question cannot be answered.

0
On

The type of a ternary conditional expression is the common type two which both operands are con­ver­tible. You can definitely not perform "dynamic overload resolution", as you seem to be suggesting.

Since there is no common type for int and char const *, the code won't even compile (as you sure­ly noticed when you tested this).

(You may be thrilled to know that the ternary conditional is used precisely because of those semantics in the implementation of the std::common_type trait class template, together with decltype.)

(If the condition is known statically, such as sizeof(int) != 7, then you can use template spe­cia­lization to write similar-looking code that does perform conditional overload resolution, but of course statically.)

0
On

No, this is not permitted.

Overloads are compile-time, so it cannot work in runtime that way.

It is not common in code you would want to do exactly that, however sometimes with iostream there is a desire to do something like:

os << ( condition ? var1 : var2 )

where var1 and var2 have different types. That also doesn't work.

You could do:

MyClassInstance->Foo( booleanValue ? boost::any(24) : boost::any("a string") );