-4
ssc_event_cb_ts get_ssc_event_cb()
{
   return 
        [this](const uint8_t *data, size_t size, uint64_t ts)
        {
          UNUSED_VAR(ts);
          handle_ssc_event(data, size);
        };
}

I can guess that the function get_ccs_event_cb is to return an anonymous function. If I want to learn more about this kind of declaration, what topic should I learn?

Jarod42
  • 203,559
  • 14
  • 181
  • 302

3 Answers3

4

The get_ssc_event_cb() is returning a lambda, where [this] is the lambda's captures list. The lambda is capturing the this pointer of the object that get_ssc_event_cb() is being called on, so the lambda can call this->handle_ssc_event() when the lambda itself is called, eg:

someType obj;
auto cb = obj.get_ssc_event_cb();
cb(data, size, ts); // calls obj.handle_ssc_event(data, size);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

You have a function returning a lambda. The expression inside [] in this context is known as lambda capture. It enables using specific variables from the surrounding scope inside the lambda.

Specifically, [this] means that get_ssc_event_cb() is a function member of a class, and the lambda can access members of the class. Apparently, handle_ssc_event() is such a member.

Eugene
  • 6,194
  • 1
  • 20
  • 31
0

I found a website: Capture *this in lambda expression: Timeline of change. It's clear to learn.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Nice that you found that, but link-only answers are discouraged here, as they don't provide any useful info. SO questions are intended to be self-contained, and links can break over time. You should copy the *relevant* info from that link into your answer so it actually answers the question asked. – Remy Lebeau Feb 10 '21 at 04:33