0

I programmed in C/C++ for years, but haven't in some time, and am unfamiliar with some of the C++ code in the snippet below.

Specifically I am confused by the inner { }. What is going on here. It looks like a get method of bufferTransfer is being called, but instead of that being followed by a semi-colon, it is followed by a bracketed segment of code. Can someone kindly tell me what this construct is called, so I can read about it elsewhere?

void process (ProcessContextReplacing<float> context)
{
    context.isBypassed = bypass;

    // Load a new IR if there's one available. Note that this doesn't lock or allocate!
    bufferTransfer.get ([this] (BufferWithSampleRate& buf)
    {
        convolution.loadImpulseResponse (std::move (buf.buffer),
                                         buf.sampleRate,
                                         Convolution::Stereo::yes,
                                         Convolution::Trim::yes,
                                         Convolution::Normalise::yes);
    });

    convolution.process (context);
}
weemattisnot
  • 889
  • 5
  • 16
  • 1
    Rather than answer this for you, I'd like to enable you to answer questions like this more readily on your in the future (hopefully that doesn't come across as massively condescending, sorry). If you look at the declaration of `bufferTransfer.get`, what type does the first argument have? That should get your google going the right way. – Dr. Watson Jun 28 '21 at 04:17

2 Answers2

3

Here we call get() passing it a lambda, where [this] denotes that the lambda captures the pointer to object on which the get() method is invoked.

All what's in the {...} are the contents of the body of the unnamed lambda function object.

Lambda has the following form:

[capture1, caputre2, ...] (param1, param2, ...) { /* processing */ };
  • [] capture list
  • () parameters list
  • {} body

Where the later two are just like in regular functions. For more, you may refer to the documentation.

rawrex
  • 4,044
  • 2
  • 8
  • 24
0

but instead of that being followed by a semi-colon

The semi-colon is there. Count the parentheses. Add some whitespace to make it easier:

bufferTransfer.get(                        // 1 (
    [this] (                               //  2 (
        BufferWithSampleRate& buf
    )                                      //  2 )
    {                                      //  2 {
        convolution.loadImpulseResponse(   //   3 (
            std::move (buf.buffer),
            buf.sampleRate,
            Convolution::Stereo::yes,
            Convolution::Trim::yes,
            Convolution::Normalise::yes
        );                                 //   3 )
    }                                      //  2 }
);                                         // 1 )

As you can see, the argument list ends on the final line and is followed by a semi-colon.

What is this construct in C++ called?

Lambda

eerorika
  • 232,697
  • 12
  • 197
  • 326