简体   繁体   English

C ++使用花括号而不是赋值运算符声明和实例化范围变量

[英]C++ Declaration and instantiation of scoped variable with curly braces instead of assignment operator

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): 我正在观看Bjarne Stroustrup在C ++ 11 Style( 链接 )上的主题演讲( 链接 )(00:35:30),我很难理解以下内容(从幻灯片中复制的代码):

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. Gadget g的声明结束时,不会编译。

My questions therefore are: 因此,我的问题是:

  • Am I correct to assume that g is being instantiated? 我是否正确地假设g正在被实例化?
  • What type of object must Gadget be for this code to compile? 这个代码要编译的Gadget必须是什么类型的对象?
  • What concept is at work on this line: Gadget g {n}; 在这一行上有什么概念: Gadget g {n}; ? ie What are the curly braces after the declaration? 即声明后的花括号是什么?
  • (probably too broad, but) Why would the compiler not recognize the curly braces as valid syntax? (可能过于宽泛,但是)为什么编译器不能将花括号识别为有效语法?

Am I correct to assume that g is being instantiated? 我是否正确地假设g正在被实例化?

Yes, you are correct. 是的,你是对的。

What type of object must Gadget be for this code to compile? 这个代码要编译的Gadget必须是什么类型的对象?

Any type that can be initialized from an int . 可以从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. 例如,如果你的Gadget类有一个构造函数接受一个int ,或者采取一些可以直接从int初始化的东西,那么代码就会被编译。

What concept is at work on this line: Gadget g {n}; 在这一行上有什么概念: Gadget g {n}; ? ie 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): 它消除了一些令人讨厌的问题,括号表示法会使C ++编译器将以下内容解析为函数声明(而不是对象的初始化):

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 . 在上面的例子中,程序员的意图可能是构造一个Gadget类型的对象g并从一个临时Widget对象初始化它:但是,编译器会将其解析为一个名为g的函数的声明,它返回一个Gadget和将一个(指向一个)函数作为参数,不接受任何参数并返回一个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. 这很可能是因为您没有使用符合C ++ 11的编译器。 You should be using one, and use the -std=c++11 or -std=c++0x compilation flag to enable C++11 support. 您应该使用一个,并使用-std=c++11-std=c++0x编译标志来启用C ++ 11支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM