2

It's been a while since I read C++ code. There are some syntax that I have no idea what it means and how to search for what it does. Can someone please explain to me what these line of code does? Specifically, I don't understand what happens in [this, 0] { myFunc(); }

class MyClass {
  public:
    vector<thread> threads;

    MyClass() {
      threads.emplace_back([this, 0] { myFunc(); });
    }
}
Nam Thai
  • 841
  • 1
  • 10
  • 26
  • 3
    It's a [lambda](https://en.cppreference.com/w/cpp/language/lambda). – super Oct 26 '19 at 02:26
  • 1
    Does the above even compile? I didn't think you could capture a literal number in a lambda (ala `[this, 0]`) – selbie Oct 26 '19 at 03:34
  • @selbie it certainly wouldn't make sense to in my brain, but honestly I'm not sure. [This question](https://stackoverflow.com/questions/19828526/is-it-possible-to-pass-a-literal-value-to-a-lambda-unary-predicate-in-c) is close, but doesn't doesn't answer that explicitly. –  Oct 26 '19 at 04:14
  • @selbie: Not according to 8.1.5.2: _"The body of a lambda-expression may refer to variables with automatic storage duration and the *this object (if any) of enclosing block scopes by capturing those entities, as described below"_. No mention of literals, so... no. – Damon Oct 26 '19 at 09:04

0 Answers0