3

On their webpage, the Nana GUI people give this example:

#include<nana/gui.hpp>

int main()
{
     using namespace nana;
     form fm;
     drawing{fm}.draw([](paint::graphics& graph){
         graph.string({10, 10}, L"Hello, world!", colors::red);
     });
     fm.events().click(API::exit);
     fm.show();
     exec();
}

What is the C++ feature that is being used in the line that starts: drawing{fm}.draw...

I have never seen {} used like that.

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
Scooter
  • 6,802
  • 8
  • 41
  • 64
  • Thinking about readability. Yeah, I *can* read (and understand) it. But doing so I also stumbled a bit in the line `drawing{fm}...`. You are not alone, Scooter. – TobiMcNamobi Jul 02 '15 at 09:31

1 Answers1

7

It's called uniform initialization, and was added in C++11. See e.g. Bjarne's page for more info.

In your particular example, an unnamed instance of drawing is constructed with fm as the actual parameter to drawing's constructor. The draw method is then invoked on this drawing instance. This could also have been written using normal parens:

drawing(fm).draw([](paint::graphics& graph){
     graph.string({10, 10}, L"Hello, world!", colors::red);
 });

With a few minor differences: namely that using {} would favor a constructor that takes an initializer_list (if such a constructor exists); using {} avoids the Most Vexing Parse; and using {} avoids implicit lossy narrowing of arguments. See this GotW for more details.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • 2
    So a temporary or unnamed "drawing" object is being constructed, and the constructor is passed the fm variable and then that new object is having it's draw() function called. Is that correct? – Scooter Jul 02 '15 at 09:13