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

后端 未结 5 1378
挽巷
挽巷 2021-01-31 19:35

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

My code like this:

#include 

        
相关标签:
5条回答
  • 2021-01-31 19:38

    It's a strong type language and it need to treat the whole ?: as a variable or statement, like:

    int i = 3;
    int j = 5;
    int k = (2 > 1 ? i : j) + 3;
    

    in such a case, what would you expect it should do for you if i is a string?

    0 讨论(0)
  • 2021-01-31 19:44

    the ?: expression will produce a value(called rvalue), which is to be assigned to a variable(which is called lvalue), as J.N. metioned, C++ is a statically typed language, lvalue must be known at compile time.

    int num = a>b ? a : "eh, string?";
    

    The code above won't compile, cause the variable num can only hold an int, string is not allowed.

    0 讨论(0)
  • 2021-01-31 19:46

    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;
    
    0 讨论(0)
  • 2021-01-31 19:52

    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.

    0 讨论(0)
  • 2021-01-31 19:59

    They don't really have to be the same type. For example, an expression like: int a = argc > 1 ? 2 : 'a'; is entirely permissible, even though 2 is of type int, and 'a' is of type char.

    There does have to be an implicit conversion to the same type though. The reason is simple: the operator has to yield one value, and the compiler has to be able to figure out the type of that value. If there's no implicit conversion between the two types, there's no one type for the expression to produce.

    0 讨论(0)
提交回复
热议问题