聚合主题的隐式初始化
Implicit initialization of aggregate subpbject
在查看 N2310 聚合的初始化规则时,我发现了以下段落(emp.mine):
6.7.9(p19)
:
The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject; 154) all
subobjects that are not initialized explicitly shall be initialized
implicitly the same as objects that have static storage duration.
6.7.9(p21)
:
If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.
在我看来,6.7.9(p19)
意味着 6.7.9(p21)
(但反之则不然)。例如:
#include <stdio.h>
struct test{
int a;
int b;
int c;
};
int main(){
struct test test = {
.a = 123
};
printf("%d%d%d\n", test.a, test.b, test.c); //obviously, 12300 is printed
}
我认为这个案例可以用6.7.9(p19)
和6.7.9(p21)
来解释
6.7.9(p21)
的目的是什么?我错过了什么?
6.7.9 考虑了所谓的指定初始化程序。
考虑以下演示程序
#include <stdio.h>
struct test{
int a;
int b;
int c;
};
int main(void)
{
struct test test = {
.c = 123
};
printf("%d %d %d\n", test.a, test.b, test.c);
return 0;
}
它的输出是
0 0 123
或其他程序
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] = { [0] = 0, [4] = 4, [9] = 9 };
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
它的输出是
0 0 0 0 4 0 0 0 0 9
所以
all subobjects that are not initialized explicitly shall be
initialized implicitly the same as objects that have static storage
duration.
第二个引用在 C 标准中采用指定的初始值设定项之前就已经存在,并且描述了例如这样的情况
char s[20] = "Hello";
并回答是否初始化字符数组尾部的问题。它保持与 C++ 标准的兼容性,在 C++ 17 标准之前没有指定的初始化器。
在查看 N2310 聚合的初始化规则时,我发现了以下段落(emp.mine):
6.7.9(p19)
:
The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; 154) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
6.7.9(p21)
:
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
在我看来,6.7.9(p19)
意味着 6.7.9(p21)
(但反之则不然)。例如:
#include <stdio.h>
struct test{
int a;
int b;
int c;
};
int main(){
struct test test = {
.a = 123
};
printf("%d%d%d\n", test.a, test.b, test.c); //obviously, 12300 is printed
}
我认为这个案例可以用6.7.9(p19)
和6.7.9(p21)
6.7.9(p21)
的目的是什么?我错过了什么?
6.7.9 考虑了所谓的指定初始化程序。
考虑以下演示程序
#include <stdio.h>
struct test{
int a;
int b;
int c;
};
int main(void)
{
struct test test = {
.c = 123
};
printf("%d %d %d\n", test.a, test.b, test.c);
return 0;
}
它的输出是
0 0 123
或其他程序
#include <stdio.h>
int main(void)
{
enum { N = 10 };
int a[N] = { [0] = 0, [4] = 4, [9] = 9 };
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
它的输出是
0 0 0 0 4 0 0 0 0 9
所以
all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
第二个引用在 C 标准中采用指定的初始值设定项之前就已经存在,并且描述了例如这样的情况
char s[20] = "Hello";
并回答是否初始化字符数组尾部的问题。它保持与 C++ 标准的兼容性,在 C++ 17 标准之前没有指定的初始化器。