什么是聚合初始化 [英] what is aggregate initialization

查看:116
本文介绍了什么是聚合初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的思想,第二版的第4章,第231页的数组初始化部分中这样说:

The section "Array Initialization" in Chapter 4, page 231 of "Thinking in Java, 2nd Edition" has this to say:


在C中初始化数组容易出错且乏味。 C ++使用
聚合初始化使其更加安全。 Java没有像C ++那样的
聚合,因为一切都是Java中的对象。
确实具有数组,并且数组初始化支持这些数组。

Initializing arrays in C is error-prone and tedious. C++ uses aggregate initialization to make it much safer. Java has no "aggregates" like C++, since everything is an object in Java. It does have arrays, and these are supported with array initialization.

为什么在C语言中容易出错且乏味?聚合初始化是什么意思?为什么它更安全?我遇到了 Bruce Eckel的 C ++中的思想 (第二版)中的聚合初始化一章,但它并没有说服我。

Why is it error prone and tedious in C? What does it mean by aggregate initialization and why is it safer? I came across the chapter "Aggregate initialization" in Bruce Eckel's "Thinking in C++" (2nd Ed.), but it doesn't convince me of anything.

推荐答案

首先,要回答主要问题,聚集初始化意味着使用大括号括起来的初始化列表来初始化聚集的所有成员(即数组或结构[在C ++中,只有某些类型的结构才算作集合])。

First of all, to answer the main question, aggregate initialization means the use of brace-enclosed initializer lists to initialize all members of an aggregate (i.e. an array or struct [in C++, only certain types of structs count as aggregates]).

很显然,

int ar[] = { 1 , 2 };

int ar[2];
ar[0] = 1;
ar[1] = 2;

因为后者为输入错误和其他错误提供了足够的机会初始化单个元素的索引

because the latter gives ample opportunity for typos and other errors in the indices of the individual elements to be initialized.

就当今的C和C ++而言,我不清楚作者为什么区分C和C ++。两种语言都可以对数组进行聚合初始化。

Looking at today's C and C++, it's unclear to me why the author makes a distinction between C and C++. Both languages enable aggregate initialization for arrays.

一种可能性是作者引用了C标准的旧版本。值得注意的是,在ANSI C(C89)中,对聚合初始化的使用施加了重要限制:所有初始化程序都必须为常量表达式:

One possibility is that the author referred to old versions of the C Standard. Notably, in ANSI C (C89) an important restriction applied to the use of aggregate initialization: All initializers had to be constant expressions:

/* This is possible in C89: */
f(int i)
{ int ar[] = { 1 , 2 }; }

/* But this is not
   (because i is not a constant expression):
*/
f(int i)
{ int ar[] = { i , i+1 }; }

这是由于C89中的3.5.7(引用了我发现的草案此处):

This is due to 3.5.7 in C89 (quoting from the draft I found here):


具有静态存储持续时间的对象的初始化程序中或具有聚合或联合类型的对象的初始化程序列表中的所有表达式均应为常量表达式。

All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

这明显限制了聚合初始化的用途(甚至在1989年,我相信许多编译器都实现了扩展,以使非常数表达式也可以进行聚合初始化)。

This clearly limits the usefulness of aggregate initialization (and even in 1989, I believe many compilers implemented extensions to enable aggregate initialization also for non-constant expressions).

C标准的更高版本没有此限制,而且我认为C ++(从C ++ 98开始)的标准化版本也没有任何限制。

Later versions of the C Standard did not have this restriction, and the standardized versions of C++ (starting with C++98), I believe, never had any such restriction.

我只能推测,但这也许是作者的初衷吗?

I can only speculate, but perhaps this is what the author had in mind?

这篇关于什么是聚合初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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