C ++使用大括号而不是赋值运算符声明和实例化范围变量 [英] C++ Declaration and instantiation of scoped variable with curly braces instead of assignment operator

查看:128
本文介绍了C ++使用大括号而不是赋值运算符声明和实例化范围变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看Bjarne Stroustrup的C ++ 11风格主题演讲(链接)(00:35:30),并且在理解以下内容(从幻灯片中复制代码)时遇到麻烦:

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;
      // ...
}

我尝试使用struct和对象,但在两种情况下,编译器都告诉我,它在 Gadget g 声明的末尾期待';'并且不会编译。

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.

因此,我的问题是:


  • 我正确地假设 g 正在实例化?

  • 此代码 Gadget 必须是哪种类型的对象?

  • 此行上有什么概念在起作用: Gadget g {n}; ?即声明后的花括号是什么?

  • (可能太宽了,但是)为什么编译器不能将花括号识别为有效的语法?

  • 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?

推荐答案


我正确地假设g被实例化了吗?

Am I correct to assume that g is being instantiated?

是的,您是正确的。


什么类型的对象必须 Gadget 是否可以编译此代码?

What type of object must Gadget be for this code to compile?

可以从 int 。例如,如果您的 Gadget 类的构造函数采用 int 或采用可以直接从 int ,使代码得以编译。

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.


什么概念在起作用行:小工具g {n}; ?即声明后的花括号是什么?

What concept is at work on this line: Gadget g {n};? i.e. What are the curly braces after the declaration?

这是统一的初始化语法。它消除了括号表示法带来的一些麻烦问题,该问题使C ++编译器将以下内容解析为函数声明(而不是对象的初始化):

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

在上面的示例中,程序员的意图可能是构造类型为 Gadget 的对象 g 并从临时 Widget 对象:但是,编译器会将其解析为声明为 g 的函数的声明,该函数返回 Gadget 并将不接受任何参数的(指向a的)函数作为参数,并返回 Widget 。这就是 最烦人的解析 问题。

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?

最有可能是因为您未使用兼容C ++ 11的编译器。您应该使用一个,并使用 -std = c ++ 11 -std = c ++ 0x 编译标志以启用C ++ 11支持。

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.

这篇关于C ++使用大括号而不是赋值运算符声明和实例化范围变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆