0

I used ([]{}) like the above, but I can't find where this grammar came from, or if there is such a thing in the function even if I press F3.

Is it a kind of function pointer to use as above?

Like this:

1.
thread t([&] { result = 1 + 2; });
2.     
shared_ptr<thread> thread(new thread([&]() {
    while (true)
    {
        int n;
        {
            lock_guard<recursive_mutex> num_lock(num_mutex);
            n = num;
            num++;
        }

        if (n >= MaxCount)
            break;

        if (IsPrimeNumber(n))
        {
            lock_guard<recursive_mutex> primes_lock(primes_mutex);
            primes.push_back(n);
        }
    }
}));

As far as I know, function pointers have a form of void(*bp2)().

Since it is in the form ([]{}), I wonder what kind of grammar it is..

Tsyvarev
  • 52,146
  • 16
  • 84
  • 120
  • 1
    Does this answer your question? [What does \[&\] mean before function?](https://stackoverflow.com/questions/39789125/what-does-mean-before-function) – Tsyvarev Dec 09 '21 at 16:45
  • yes that seems right Thanks to you, I know the keywords I need to search. thank you. – Hanbyul Jung Dec 09 '21 at 17:54
  • It's a lambda. The [ ] is a "closure". The { } is the body. Inside the closure you can capture variables from the surrounding scopes. These variables get bound to the lambda and can be used inside the body. In one of your examples there are no ( ). This means ( ) by default (no args). This has the same call signature and return value as `void(*)()`. A lambda isn't a function pointer (although it can be called like one). A function pointer is a simple pointer. A lambda is an instance of a compiler-generated class. They are aggressively inlined. – Humphrey Winnebago Dec 09 '21 at 23:30

0 Answers0