1

I have the following code:

auto x_is_valid = [](const MyX &x) -> bool {
    return x.source != MyXValue::ABC;
};

auto objects = var_.var_in_box(*a, b, c, x_is_valid);

I am wondering:

  • How is x_is_valid computed?
  • Where does it take its input parameter?

How do I read this properly?

Thanks!

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Edamame
  • 23,718
  • 73
  • 186
  • 320
  • It's not being computed, its being passed to `var_in_box`. You need to look at that code to see how it is used. – NathanOliver Jan 09 '19 at 22:53
  • `x_is_valid` is a _functor_, that is, it's an object that overloads the function call operator `operator()`. It can be passed around like any other object. Here's a good read about what lambdas are and how they work: https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 – alter_igel Jan 09 '19 at 22:54
  • Also see: https://stackoverflow.com/questions/356950/what-are-c-functors-and-their-uses – NathanOliver Jan 09 '19 at 22:54

2 Answers2

2

A mathematical analogy might help. Imagine a function f(x) = x^2.

How is f computed?

It's right there: for any x, the formula for computing is f(x) = x^2.

Where does it take its input parameter?

From the caller.

The "answers" above are pretty pointless, but if you understand them in a context of a function in a mathematical sense, they might be helpful.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

If var_.var_in_box, expects a bool as the last argument, then the call

auto objects = var_.var_in_box(*a, b, c, x_is_valid);

should result in a compile error.

If the above line compiles without any error, then the last argument type of the above is a callable object, not a bool. Presumably, the function uses the passed in callable object to make a function call. It's not possible to determine from the posted code how the callable is called in the implementation of var_in_box member function of the class.

R Sahu
  • 204,454
  • 14
  • 159
  • 270