为什么他们特殊的初始化列表而不是一样的? [英] Why would they special-case certain initializer lists instead of treating them all the same?
问题描述
假设我有一个变量 auto x
,我想使用大括号初始化初始化为 7
/ p>
Say I have a variable auto x
that I want to initialize to 7
using brace initialization, simple:
auto x {7};
除非我知道x是 NOT 一个整数,本身。为什么?是否有一个具体原因,为什么委员会会决定 auto
应该在单个自动值的情况下抓取初始化列表,或者他们希望我们只是实现这些 t一起使用。 我似乎没有想到一个可能的原因,我想要一个初始化列表存储到自动而不是值
Except I learned that x is NOT an integer, but an initialization list itself. Why? Is there a specific reason why the committee would decide that auto
should grab the initialization list in the case of a single auto value, or do they expect us to just realize these shouldn't be used together. I cant seem to think of a possible reason i would want an initializer list to be stored into auto as opposed to the value
推荐答案
一个非常实用的答案是,为什么应该选择 int
?或 double
,或任何带有int单参数构造函数的UDT?通过拒绝推导一个具体类型,编译器保留了更一般的初始化列表的任何可能的应用。
A very practical answer is, "why should int
be selected?" Or double
, or any UDT with an int single-argument constructor? By declining to deduce a concrete type, the compiler preserves any possible application of the more general initializer list.
这篇关于为什么他们特殊的初始化列表而不是一样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!