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:
g
is being instantiated?Gadget
be for this code to compile?Gadget g {n};
? i.e. What are the curly braces after the declaration?Am I correct to assume that g is being instantiated?
Yes, you are correct.
What type of object must
Gadget
be for this code to compile?
Any type that can be initialized from an int
. For instance, if your Gadget
class has a constructor taking an int
, or taking something that can be initialized directly from an int
, that makes the code compile.
What concept is at work on this line:
Gadget g {n};
? i.e. What are the curly braces after the declaration?
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):
struct Widget { /* ... */ };
struct Gadget { Gadget(Widget const&) { /* ... */ } /* ... */ };
Gadget g(Widget()); // This is parsed a FUNCTION DECLARATION
In the above example, the intent of the programmer may have been to construct an object g
of type Gadget
and initialize it from a temporary Widget
object: however, the compiler would parse this as the declaration of a function called g
that returns a Gadget
and takes as its argument a (pointer to a) function that accepts no arguments and returns a Widget
. This is known as the Most Vexing Parse problem.
Notice, that when using braces the above problem does not exist:
Gadget g{Widget{}}; // This could not be possibly parsed as a function declaration!
(probably too broad, but) Why would the compiler not recognize the curly braces as valid syntax?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With