为什么必须< initializer_list>包括使用汽车? [英] Why must <initializer_list> be included for using auto?

查看:166
本文介绍了为什么必须< initializer_list>包括使用汽车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有类似的问题 ,但我想强调的另一个方面的支撑初始化列表。请考虑以下内容:

There has already been a similar question on SO, but I want to stress another aspect of braced-init-lists. Consider the following:

auto x = {1}; //(1)

这是错误的(8.5.4 / 2) code>< initializer_list> 。但为什么?标准说,模板 std :: initializer_list 未预定义。这是否意味着,声明(1)引入了一个新类型?在所有其他情况下,可以使用 auto ,例如

This is ill-formed (8.5.4/2) unless the header <initializer_list> is included. But why? The standard says, that the template std::initializer_list is not predefined. Does this mean, that declaration (1) introduces a new type? In all other situations, where auto may be used such as

auto y = expr;

其中 expr 是一个表达式,类型自动推演已存在。另一方面,从逻辑角度来看,编译器必须为构造 {1} 分配 implicite类型,其中 std :: initializer_list 是另一个名称。但在声明(1)中,我们不想命名此类型。所以为什么必须包括这个标题。也有类似的情况, nullptr 。它的类型隐含存在,但是明确地命名它必须包括< cstddef>

where expr is an expression, the type auto deduces already exists. On the other hand, from a logical point of view, the compiler must assign an implicite type to the construct {1}, for which std::initializer_list is then another name. But in declaration (1) we do not want to name this type. So why must this header be included. There is a similar situation with nullptr. Its type implicitely exists, but to name it explicitely you have to include <cstddef>.

推荐答案

这不一样。 std :: nullptr_t std :: initializer_list 的规则实际上是不同的。

That's not the same. The rules for std::nullptr_t and std::initializer_list are actually different.

std :: nullptr_t 只是内置类型的typedef。它的定义是

std::nullptr_t is just a typedef for a built-in type. Its definition is

namespace std {
  using nullptr_t = decltype(nullptr);
}

类型是否包含头。

std :: initializer_list 是类模板,而不是预定义类型。它真的不存在,除非你包括定义它的头。特别是,初始化列表 {1} 没有类型 std :: initializer_list< int> 它没有类型,因为它不是一个表达式。 (初始化器列表是特殊的句法结构,不能在表达式的任何地方出现。)

std::initializer_list is a class template, not a predefined type. It really doesn't exist unless you include the header that defines it. In particular, the initializer list { 1 } does not have type std::initializer_list<int>; it has no type at all, because it is not an expression. (Initializer lists are special syntactic constructs and cannot appear everywhere an expression can.)

std :: initializer_list 稍微特别。对于一个,有一些特殊的规则,如何从初始化器列表语法(分配数组并让对象引用它)初始化 std :: initializer_list 。但是,这需要首先定义<$ p $ c> std :: initializer_list 。

std::initializer_list is just slightly special. For one, there are special rules for how to initialize a std::initializer_list from the initializer list syntax (allocate an array and have the object refer to it). However, this requires std::initializer_list to be defined in the first place.

第二种特殊情况是 auto 类型扣除。这里也有一个特殊的规则。但同样,这并不意味着编译器会自动定义类型;它只是意味着它会认出它。

The second special case is auto type deduction. There's a special rule here too. But again, this doesn't mean that the compiler will automatically define the type; it just means that it will recognize it.

这篇关于为什么必须&lt; initializer_list&gt;包括使用汽车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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