C ++ Style用于初始化单个变量 [英] C++ Style for the initialization of single variables

查看:107
本文介绍了C ++ Style用于初始化单个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在初始化自动管理的简单变量而不是通过赋值时,有几种不同的样式。我想知道是否有任何具体的理由来支持一个或另一个,或者只是一个风格问题。



使用括号是有吸引力的,因为它感觉类似于实例化对象

 双重答案(42.0); 
ComplexNumber i(0,1);

使用大括号是有吸引力的,因为它感觉类似于初始化容器

 双回答{42}; 
std :: vector< double> i {0,1};
double i2 [] = {0,1};

有什么特别的理由偏爱一种风格吗?

解决方案

查看这里: GotW#1:Variable初始化。它是H.Sutter的回答的详细描述。



H。 Sutter在一般意义上描述了变量初始化的新旧样式。



根据你的开始话题上下文,下面显示了一篇来自文章的简要概要。 / p>




此对

  double answer(42.0); //(1)
double answer {42}; //(2)

其实,类似于下一个初始化示例:

 小部件w(x); //(d)
widget w {x}; //(e)

这些都是直接初始化。但是,请注意语法 {x} 创建一个 initializer_list 。如果 widget 有一个构造函数,它接受 initializer_list ,那么该构造函数是首选;否则,如果 widget 有一个构造函数,它接受任何类型 x (可能带有转换),则使用该构造函数。



有两个主要区别使(2,e)优于(1,d):




  • 首先,语法(2,e)是明确的,并避免vexing parse。如果 x 是类型名称,则(1,d)是一个函数声明,即使还有一个名为 x
  • 其次,语法(2,e)更安全,因为它不允许收缩(也称为有损)转换,否则允许某些内置类型。考虑:



    int i1(12.345); //确定:折腾.345,我们还是不喜欢



    int i2 {12.345} //错误:将是有损隐式缩小

    $




b
$ b

  ComplexNumber i(0,1); //(3)
std :: vector< double> i {0,1}; //(4)

与复杂对象的初始化绑定。两者看起来完全相同,但第二个帮助我们避免烦人解析,例如:

  ComplexNumber w ),img()); // oops,vexing parse 

除此之外,这种方式使代码更清晰(如果我们使用 initializer_list ,它更清楚的是初始化),
,此外,在某些情况下减轻语法,例如:

  draw_rect({origin,selection}); // C ++ 11 

Sutter guildeline is:prefer to use initialization with {} ,例如 vector< int> v = {1,2,3,4}; auto v = vector< int> {1,2,3,4}; ,因为它更一致,更正确,并避免不得不了解老式的陷阱。在单参数的情况下,你更喜欢只看到 = 符号,例如 int i = 42 auto x = anything ;



我们可以使用() - 初始化来显式调用特殊的构造函数。


There are a couple of differing styles when initializing automatically managed simple variables other than via assignment. I was wondering if there are any specific reasons to favour one over the other or is it just a matter of style.

Using parentheses is appealing because it feels similar to instantiating an object

double answer(42.0);
ComplexNumber i(0,1);

while using braces is appealing because it feels similar to initialising a container

double answer{42};
std::vector<double> i{0,1};
double i2[] = {0,1};

Is there any particular reason to favour one style over the other?

解决方案

Look here: GotW #1 : Variable Initialization. It's a detailed description of answer from H. Sutter.

H. Sutter talks above in a general sense of old and new styles of Variable Initialization.

Below is showed a quick synopsis from the article, according to your start-topic context.


This pair

double answer(42.0); // (1)
double answer{42};  // (2)

in fact, is similar to the next initialization example:

widget w(x);                // (d)
widget w{x};                // (e)

These are both direct initialization. However, note that the syntax {x} creates an initializer_list. If widget has a constructor that takes an initializer_list, that constructor is preferred; otherwise, if widget has a constructor that takes whatever type x is (possibly with conversions), that constructor is used.

There are two major differences that make (2, e) superior to (1, d):

  • First, syntax (2, e) is unambiguous and avoids the "vexing parse". If x is a type name, then (1, d) is a function declaration even if there is also a variable named x in scope (see above), whereas (2, e) is never a function declaration.
  • Second, syntax (2, e) is safer because it does not allow narrowing (a.k.a. "lossy") conversions that are otherwise allowed for some built-in types. Consider:

    int i1( 12.345 ); // ok: toss .345, we didn't like it anyway

    int i2{ 12.345 }; // error: would be lossy implicit narrowing

Next pair

ComplexNumber i(0,1); // (3)
std::vector<double> i{0,1}; // (4)

is tied with initialization of complex objects. Both looks quite the same, but the second one helps us to avoid "vexing parse", like:

ComplexNumber       w( real(), img() );       // oops, vexing parse 

Besides of that, this way make code more clear (if we use initializer_list, it's more clear that's initialization), and, moreover, alleviate syntax in some cases, for instance:

draw_rect({ origin, selection });                  // C++11

Sutter guildeline is: prefer to use initialization with { }, such as vector<int> v = { 1, 2, 3, 4 }; or auto v = vector<int>{ 1, 2, 3, 4 };, because it’s more consistent, more correct, and avoids having to know about old-style pitfalls at all. In single-argument cases where you prefer to see only the = sign, such as int i = 42; and auto x = anything; omitting the braces is fine.

We can use ()-initialization for the explicit call of the special constructor.

这篇关于C ++ Style用于初始化单个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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