Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in conditional operator (?:), second and third operands must have the same type?

Tags:

Why in conditional operator(?:), second and third operands must have the same type?

My code like this:

#include <iostream> using std::cout;  int main() {     int a=2, b=3;     cout << ( a>b ? "a is greater\n" : b );  /* expression ONE */     a>b? "a is greater\n" : b;               /* expression TWO */      return 0; } 

When compile it using g++, it issue an error:

main.cpp:7:36: error: operands to ?: have different types ‘const char*’ and ‘int’ main.cpp:8:28: error: operands to ?: have different types ‘const char*’ and ‘int’ 

I wonder why they must have the same type?

(1) In my opinion, if (a>b) is true, then the expression ( a>b ? "a is greater\n" : b )will return "a is greater\n" and cout << "a is greater\n" will output that string;
otherwise the expression will return b, and cout << b will output the value of b.

But, unfortunately it is not like this. WHY?

(2) The second expression gets the same error.

PS: I know, it is the standard who says it must be like this, but, why the standard say so?

like image 804
Yishu Fang Avatar asked Mar 12 '12 04:03

Yishu Fang


People also ask

How is the type of a conditional expression determined when its operands differ in type?

If both operands are of type void, the common type is type void. If both operands are of the same user-defined type, the common type is that type. If the operands have different types and at least one of the operands has user-defined type then the language rules are used to determine the common type.

How many operands Does the conditional operator need?

Since the Conditional Operator '?:' takes three operands to work, hence they are also called ternary operators. Working: Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then Expression2 will be executed and the result will be returned.

What are the two types of conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

What is the purpose of conditional operator?

A conditional operator is a single programming statement, while the 'if-else' statement is a programming block in which statements come under the parenthesis. A conditional operator can also be used for assigning a value to the variable, whereas the 'if-else' statement cannot be used for the assignment purpose.


2 Answers

You should try to decompose what's happening to understand:

cout << ( a>b ? "a is greater\n" : b ); 

This translates to :

operator<<(cout, ( a>b ? "a is greater\n" : b )); 

At that stage the compiler must choose one of the following overloads:

ostream& operator<<(ostream&, int); ostream& operator<<(ostream&, const char*); 

But it can't because the type of result of the ternary operator is not known yet (only at runtime).

To make things clearer think like this:

 auto c = a>b ? "test" : 0; 

What would be the type of c ? It can't be decided at compile time. C++ is a statically typed language. All types must be known at compile time.

EDIT:

You are thinking of a ? b : c in the following way:

if (a)     b; else     c; 

While it is actually this:

if (a)     return b; else     return c; 
like image 126
J.N. Avatar answered Nov 09 '22 21:11

J.N.


I wonder why they must have the same type?

In C++ any expression must have a single type, and the compiler should be able to deduce it at compile time.

This stems from the fact that C++ is a statically typed language wherein all types must be known at compile time.

like image 23
Alok Save Avatar answered Nov 09 '22 20:11

Alok Save