何时使用括号括起来的初始化器? [英] When to use the brace-enclosed initializer?

查看:168
本文介绍了何时使用括号括起来的初始化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,我们有了初始化类的新语法,它为我们提供了大量的初始化变量的可能性。

In C++11, we have that new syntax for initializing classes which gives us a big number of possibilities how to initialize variables.

{ // Example 1
  int b(1);
  int a{1};
  int c = 1;
  int d = {1};
}
{ // Example 2
  std::complex<double> b(3,4);
  std::complex<double> a{3,4};
  std::complex<double> c = {3,4};
  auto d = std::complex<double>(3,4);
  auto e = std::complex<double>{3,4};
}
{ // Example 3
  std::string a(3,'x');
  std::string b{3,'x'}; // oops
}
{ // Example 4
  std::function<int(int,int)> a(std::plus<int>());
  std::function<int(int,int)> b{std::plus<int>()};
}
{ // Example 5
  std::unique_ptr<int> a(new int(5));
  std::unique_ptr<int> b{new int(5)};
}
{ // Example 6
  std::locale::global(std::locale("")); // copied from 22.4.8.3
  std::locale::global(std::locale{""});
}
{ // Example 7
  std::default_random_engine a {}; // Stroustrup's FAQ
  std::default_random_engine b;
}
{ // Example 8
  duration<long> a = 5; // Stroustrup's FAQ too
  duration<long> b(5);
  duration<long> c {5};
}

对于每个变量,我声明,我必须考虑应该使用哪种初始化语法这会降低我的编码速度。我确定这不是引入大括号的意图。

For each variable I declare, I have to think which initializing syntax I should use and this slows my coding speed down. I'm sure that wasn't the intention of introducing the curly brackets.

当涉及模板代码时,更改语法会导致不同的含义,所以去

When it comes to template code, changing the syntax can lead to different meanings, so going the right way is essential.

我不知道是否有一个应该选择的语法的通用指南。

I wonder whether there is a universal guideline which syntax one should chose.

推荐答案

我认为以下可能是一个很好的指南:

I think the following could be a good guideline:


  • 如果要初始化的(单个)值打算作为对象的 exact value ,请使用copy( = )初始化那么在错误的情况下,你永远不会意外调用一个显式的构造函数,通常解释提供的值不同)。在复制初始化不可用的地方,看看大括号初始化是否有正确的语义,如果是,使用;

  • If the (single) value you are initializing with is intended to be the exact value of the object, use copy (=) initialization (because then in case of error, you'll never accidentally invoke an explicit constructor, which generally interprets the provided value differently). In places where copy initialization is not available, see if brace initialization has the correct semantics, and if so, use that; otherwise use parenthesis initialization (if that is also not available, you're out of luck anyway).

如果你初始化的值是一个列表,你可以使用括号初始化(如果这也不可用,要存储在对象中的值(如向量/数组的元素,或复数的实部/虚部),如果可用,使用花括号初始化。

If the values you are initializing with are a list of values to be stored in the object (like the elements of a vector/array, or real/imaginary part of a complex number), use curly braces initialization if available.

如果要初始化的值是不是值要存储,而是描述对象的预期值/状态,请使用括号。示例是向量的大小参数或 fstream 的文件名参数。

If the values you are initializing with are not values to be stored, but describe the intended value/state of the object, use parentheses. Examples are the size argument of a vector or the file name argument of an fstream.

这篇关于何时使用括号括起来的初始化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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