How to use C + + function to implement functions similar to the three-mesh operator __jquery

Source: Internet
Author: User
Problem leads

Today, Aikilis asked me two questions:
1 is the code below legal?

(i > 0 i:j) = 1;

2 How to use a prototype for Quest (Bool,type,type) function to achieve the three-character function.

On trial, the answer to the first question is yes, although it has never been used before.
The second question really cost a lot of brains. Three The nature of the eye character

void Test0 () {
        int i = 0, j = 0, K;
        (i > 0 i:j) = 3;  OK
        (i > 0 i:2) = 3;  Error
        (i > 0 1:j) = 3;  Error
        (i > 0 1:2) = 3;  Error
        k = (J > 0 i:j);  Ok
        k = (J > 0 i:2);  Ok
        k = (i > 0 1:j);  Ok
        k = (i > 0 1:2);  OK
}

That is, when the right side of the assignment, the three-character return to the left value of the right value can be.
But when placed to the left of the assignment, the three-character return value must be the left value.

So our goal is to implement the following code:

void Test1 () {
        int i = 0, j = 0, K;
        Quest (i > 0, I, j) = 3;  OK
        Quest (i > 0, I, 2) = 3;  Error
        Quest (i > 0, 1, j) = 3;  Error
        Quest (i > 0, 1, 2) = 3;  Error
        K = Quest (J > 0, I, j);  Ok
        K = Quest (J > 0, I, 2);  Ok
        k = Quest (i > 0, 1, j);  Ok
        k = Quest (i > 0, 1, 2);  OK
}
a failed attempt

1 at the beginning of the idea, through the function overload, the input is left value to return the reference to the left value, the input is the right value to return the right value.

template< TypeName T > t& quest for Left value  (bool cond, t& True_val, t& false_val) {
    if (cond) return true_val;
    return false_val;
}
template< typename T >
T  Quest (bool cond, const T true_val, const T false_val) prepared for the right value {
    if ( COND) return true_val;
    return false_val;
}

But this is not going to work. Compilation results:

void Test1 () {
        int i = 0, j = 0, K;
        Quest (i > 0, I, j) = 3;  Ambiguous, because the left value can also be used as the right value with
        quest (i > 0, I, 2) = 3;  Error
        Quest (i > 0, 1, j) = 3;  Error
        Quest (i > 0, 1, 2) = 3;  Error
        K = Quest (J > 0, I, j);  Ambiguous, because the left value can also be used as the right value with
        K = Quest (J > 0, I, 2);  Ok
        k = Quest (i > 0, 1, j);  Ok
        k = Quest (i > 0, 1, 2);  OK
}

It can be seen that there are two inconsistent with the standard answer, that is, when both True_val and False_val are left values, the compiler cannot differentiate which version is invoked.

2 Then I think of the right value reference, a check, the right value reference is really a few interesting features:
A> right value reference as function parameter, can only pass right or temporary variable, cannot pass left or left value reference
b> If you can use a reference fold in a template or a TypeDef, the collapse rules are as follows:
-Right reference to right value reference collapsed to right value reference (t&& && considered to be t&&)
-Other cases are considered to be left-value references (t& && considered to be t&)
c> If you pass the left value, when you infer that T is a prototype, it makes up the t&& right value reference, causing the binding error, when the compiler will intelligently infer that the input is t&, triggering the reference collapse, inferring that the final type of the parameter is t&

Based on the above features, I wrote a second version:

Parameter uses the right value reference, the return value uses the left value reference
template < typename T >
t& Quest (bool Cond, t&& True_val, t&& F Alse_val) {
        if (cond) return true_val;
        return false_val;
}

Compilation results:

void Test1 () {
        int i = 0, j = 0, K;
        Quest (i > 0, I, j) = 3;  OK
        Quest (i > 0, I, 2) = 3;  Error
        Quest (i > 0, 1, j) = 3;  Error
        Quest (i > 0, 1, 2) = 3;  Ok
        K = Quest (J > 0, I, j);  Ok
        K = Quest (J > 0, I, 2);  Error
        K = Quest (i > 0, 1, j);  Error
        K = Quest (i > 0, 1, 2);  OK
}

When the passed parameter is a left value of a right value, but I know what happened, but the 4th passed, that is, because the return is a reference to the left, so even if I pass in two right values, the pass is actually a temporary variable of the left value reference.

3 and then I thought about the characteristics of C above, so I wrote the return value T because the compiler would push T to the reference type for me when I passed the left value.

Template < typename T >
T Quest (bool Cond, t&& True_val, t&& false_val) {
        if (cond) retur n True_val;
        return false_val;
}

It can be said that this has solved the problem of 90%. The last remaining question is what to do when passing in a left value and a right value, and my solution is to add two functions. Final Results

Template < typename T >
T Quest (bool Cond, t&& True_val, t&& false_val) {
        if (cond) retur n True_val;
        return false_val;
}

Template < typename T >
T Quest (bool Cond, t& True_val, t&& false_val) {
        if (cond) return T Rue_val;
        return false_val;
}

Template < typename T >
T Quest (bool Cond, t&& True_val, t& false_val) {
        if (cond) return T Rue_val;
        return false_val;
}

Reference
[1] http://en.cppreference.com/w/cpp/language/reference
[2] http://www.th7.cn/ Program/cp/201403/183896.shtml
[3] http://www.2cto.com/kf/201311/260709.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.