2

What exactly does the first line here do? I'm having trouble understanding the syntax of the code on the right side of the assignment--specifically, the []() and the trailing (). This looks like some sort of nameless inline function call? I've never seen this before.

HBRUSH hBrBlack = []() { return CreateSolidBrush(COLOR_BLACK); }();

For context, this is where the handle above is used (the question is about the code above):

HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_LISTBOX)
    {
        int id(pWnd->GetDlgCtrlID());

        if (id == ID_LST_MTRS)
        {
            pDC->SetTextColor(COLOR_WHITE);
            pDC->SetBkColor(COLOR_BLACK);
            return hBrBlack;
        }
    }

    // All the rest
    return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
MPW
  • 329
  • 2
  • 13
  • 2
    Well it it just creates a lambda, calls it, then assigns the result of the call to `hBrBlack` – Captain Obvlious Dec 14 '22 at 23:09
  • 1
    @CaptainObvlious - the obvious question is whyat purpose is served by doing in such a complicated way – pm100 Dec 14 '22 at 23:10
  • 2
    And now it is time for the revenge of the nerds! [Lambda! Lambda! Lambda!](https://en.cppreference.com/w/cpp/language/lambda) – user4581301 Dec 14 '22 at 23:10
  • my guess is that the lambda was using in a more 'normal' way , but then edits were done and the code was left in this kind of funky state – pm100 Dec 14 '22 at 23:11
  • 5
    @pm100 There's no real value in it except to say *hey I'm using lambdas!* – Captain Obvlious Dec 14 '22 at 23:12
  • @pm100 - Or it's a person used to JavaScript - where such tricks are pretty common - and carrying their coding habits across into C++. – Peter Dec 14 '22 at 23:15
  • In the sample code `hBrBlack` is not defined in the function scope -- is the original code at namespace scope? (i.e. not in a function or class)? – M.M Dec 14 '22 at 23:25
  • Does this answer your question? [What is a lambda expression in C++11?](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – Isaiah Dec 15 '22 at 00:41
  • 1
    Does this answer your question? [trying to understand what "int any = \[\]() { //code body } ();" means](https://stackoverflow.com/questions/53938482/trying-to-understand-what-int-any-code-body-means) or [C++ lambda expression (anonymous function)](https://stackoverflow.com/questions/12765442/c-lambda-expression-anonymous-function) (more complicated by a parameter); there is also [What does ___ mean in C++](https://stackoverflow.com/questions/22930154/what-does-this-mean-in-c) but the latter lacks the "immediately invoked" part. – JaMiT Dec 15 '22 at 03:13
  • @M.M : Yes, it appears that hBrBlack is a global HBRUSH object. – MPW Dec 15 '22 at 16:00
  • OK. I wonder if the code was an experiment by someone struggling with static initialization order issues. – M.M Dec 15 '22 at 22:11

1 Answers1

5

[...](...){...} declares a lambda function. The trailing () then immediately calls that anonymous function.

This pattern is usually called IIFE (immediately invoked function expression). I find this pattern particularly useful if I have an object that requires multiple steps to construct/initialize but I want the object to be const.

But, in this case, IIFE seems to provide no benefit - you could just write it as:

HBRUSH hBrBlack = CreateSolidBrush(COLOR_BLACK);

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0x5453
  • 12,753
  • 1
  • 32
  • 61