How do I pass an unevaluated expression as an an argument in C++?

78 Views Asked by At

In Java I can declare a Supplier<Foo> parameter, get the value using s.get(), and create the supplier using () -> someExpression(), so the expression is only evaluated if needed:

static void doSomething(Supplier<Foo> s) {
    if (gonnaNeedThatObject) {
        Foo foo = s.get();
        // do stuff with foo
    }
}

...

doSomething(() -> someExpression());

How can I accomplish this in C++? There seems to be a few different ways, but I can't quite figure out the syntax or the benefits and drawbacks of each way.

0

There are 0 best solutions below