English 中文(简体)
C++不同初始化的类型转换:为什么允许使用“=”但“{}”产生错误?
原标题:Type conversion at variable initialization in C++: why using "=" is allowed but "{}" results in error?

顾问Stroustrup书“C++ 方案拟订语言4” 因此,我无法理解,为什么汇编者在使用旧的“=”分配方法开始变数时进行必要类型的转换:

int i1    = 7.2;      // i1 becomes 7 due truncation
bool b1   = 7;        // 7!=0, so b1 becomes true

但会给以下人员留下错误:

int i2 {7.2};       // error: floating-point to integer conversion
bool b2 {7};        // error: narrowing

Note, the question is not about truncation or narrowing, but what does the language/compiler do differently that one way it "can make it work" but the other don t.

编辑这一文件将产生以下成果:

error: narrowing conversion of ‘7’ from ‘int’ to ‘bool’ [-Wnarrowing]
   24 |         bool b2{7};
问题回答

It s due uniform initialization.

正如@marcinj在评论中指出的:

both int i2=7.2; and bool b1 = 7; could be a coding mistakes causing difficult to find bugs. Uniform initialization can help you find such places quickly at the very beggining.

Quoting

统一初始化是C++11的一个特点,它允许使用一种前后一致的合成物,以初步确定从原始类型到合计的变量和物体。 换言之,它引进了使用镜头({})的青,素初始化,以包含初始化器的价值。

More details at Iso Standard C++11: https://isocpp.org/wiki/faq/cpp11-language#uniform-init

Video Resources

事实上,问题是对“统一初始化”究竟是什么误解。

Here are some video resources that helped me get a focused understanding of initialization in C++. These are talks given by Timur Doumler and Nicolai Josuttis.

我和他们一样,但Timur Doumler的谈话能更好地解释各种初始化之间的差异。 Nicolai Josuttis的讲话更注重成果,但在某些细节上却.忙。

YouTube有4种不同的演讲版本。 他们大致相同,这样一来。 我没有看到最后一点,但也许我要开始。 简言之,它有一部分kin子。

CppCon 2018 – Nicolai Josuttis – "The Nightmare of Initialization in C++” (1:00)
Meeting C++ 2018 – Timur Doumler – "Initialization in modern C++" (1:01)
C++ on Sea 2019 – Timur Doumler – "Initialisation in modern C++ " (1:04)
Core C++ 2019 – Timur Doumler – "Initialisation in modern C++" (1:01)
C++ User Group (Russia) – Timur Doumler – "Initialisation in modern C++" (1:00)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签