基于C++初始化数组版本和编译器

发布于 2025-01-28 21:11:52 字数 417 浏览 1 评论 0原文

In C++11 or higher regardless of compiler int myArray[10] = { 0 }; would initialize to all elements to zero.问题是,这也可以在C ++ 98中起作用,并且编译器是否可以决定将所有元素初始化为零?换句话说,用给定编译器的C ++ 98可以忽略所有元素的零分配吗?

我找到了此页面: https://en.cppreferent /Zero_Initialization 列出了C ++ 98关于零初始化的缺陷。

In C++11 or higher regardless of compiler int myArray[10] = { 0 }; would initialize to all elements to zero. The question is would this also work in C++98 and could the compiler not decide to initialize all elements to zero? In other words could C++98 with a given compiler ignore the assigning zero to all elements?

I found this page: https://en.cppreference.com/w/cpp/language/zero_initialization which lists C++98 defects about zero initialization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2025-02-04 21:11:52

所有元素均为零。 C ++的引号98:

[dcl.init.aggr]

如果列表中的初始化器比聚合中的成员少,则每个成员不
显式初始化应为默认限制(8.5)


[dcl.init]

默认intialize t type t type the Mene:

  • 如果T是非POD类类型(第9条),则称为T的默认构造函数(初始化为
    如果t没有可访问的默认构造函数,则不良形式);
  • 如果t是数组类型,则每个元素均为默认限制;
  • 否则,对象的存储为零。

与从C ++ 03开始的含义相比,默认初始化的含义在C ++ 98中截然不同,在C ++ 03中,旧的默认初始化本质上是重新命名的值初始化,而新的默认初始化变成了“无初始化”的意思。

请注意,int myarray [10] = {};将实现相同的目标。不必明确地为第一个元素提供值。

All elements would be zero. Quotes from C++98:

[dcl.init.aggr]

If there are fewer initializers in the list than there are members in the aggregate, then each member not
explicitly initialized shall be default-initialized (8.5)


[dcl.init]

To default-initialize an object of type T means:

  • if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is
    ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, the storage for the object is zero-initialized.

The meaning of default initialisation was radically different in C++98 compared to its meaning starting from C++03 where the old default initialisation essentially was renamed value initialisation and the new default initialisation became to mean "no initialisation" for trivial types.

Note that int myArray[10] = {}; would achieve the same goal. It's unnecessary to explicitly provide a value for the first element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文