Strange “->* []” expression in C++ source code of cpp.react library

前端 未结 3 1495
一向
一向 2021-01-30 20:09

Here is a C++ snippet that I found in the documentation of the cpp.react library:

auto in = D::MakeVar(0);
auto op1 = in ->* [] (int in)
{
    int result = in         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-30 20:39

    The only example on the linked page where I see ->* is this:

    auto in = D::MakeVar(0);
    
    auto op1 = in ->* [] (int in)
    {
        int result = in /* Costly operation #1 */;
        return result;
    };
    
    auto op2 = in ->* [] (int in)
    {
        int result = in /* Costly operation #2 */;
        return result;
    };
    

    Here's my guess - whatever type is returned by D::MakeVar() overloads the pointer-to-member operator ->*, and the second argument for that overloaded operator is a function object, i.e. the lambda expression.

    As for this example:

    auto volume = (width,height,depth) ->* [] (int w, int h, int d) {
        return w * h * d;
    };
    

    I'm guessing whatever types width, height & depth are, overload the comma operator, and the result yields the same type as what MakeVar yields, or another type that overloads ->*. The rest is the same as the first example.

提交回复
热议问题