I am watching Bjarne Stroustrup's Keynote on C++11 Style (link) (00:35:30) and am having troubles understanding the following (code copied from the slide):
void f(int n, int x)
{
Gadget g {n};
// ...
if (x<100) throw std::run_time_error{"Weird!"};
if (x<200) return;
// ...
}
I tried compiling this code using a struct as well as an object but in both cases the compiler tells me that it is expecting a ';' at the end of the declaration of Gadget g
and won't compile.
My questions therefore are:
- Am I correct to assume that
g
is being instantiated? - What type of object must
Gadget
be for this code to compile? - What concept is at work on this line:
Gadget g {n};
? i.e. What are the curly braces after the declaration? - (probably too broad, but) Why would the compiler not recognize the curly braces as valid syntax?
Yes, you are correct.
Any type that can be initialized from an
int
. For instance, if yourGadget
class has a constructor taking anint
, or taking something that can be initialized directly from anint
, that makes the code compile.That's uniform initialization syntax. It eliminates some nasty problem with the parentheses notation that would make the C++ compiler parse the following as a function declaration (rather than as the initialization of an object):
In the above example, the intent of the programmer may have been to construct an object
g
of typeGadget
and initialize it from a temporaryWidget
object: however, the compiler would parse this as the declaration of a function calledg
that returns aGadget
and takes as its argument a (pointer to a) function that accepts no arguments and returns aWidget
. This is known as the Most Vexing Parse problem.Notice, that when using braces the above problem does not exist:
That's most likely because you are not using a C++11-compliant compiler. You should be using one, and use the
-std=c++11
or-std=c++0x
compilation flag to enable C++11 support.