放置在具有自动存储持续时间合法的常量变量上是新的吗?

发布于 2025-01-28 15:26:42 字数 416 浏览 4 评论 0原文

以下代码是根据标准合法的吗?

#include <new>
int main() {
    const int x = 3;
    new ((void *)&x) int { 15 };
}

在我看来,只要不使用对x的引用,就应该有效。

根据C ++标准基本。Life8:

指向原始对象的指针,一个引用 转介到原始对象[...]

  • 原始类型 对象不合格,如果类型类型不包含 任何类型为constemified或a的非静态数据成员 参考类型

PS:如果答案可以包含对标准的参考,将不胜感激

Is the following code legal according to the standard?

#include <new>
int main() {
    const int x = 3;
    new ((void *)&x) int { 15 };
}

It seems to me that as long as there is no use of a reference to x it should be valid.

As per the c++ standard basic.life 8:

a pointer that pointed to the original object, a reference that
referred to the original object [...]

  • the type of the original
    object is not const-qualified, and, if a class type, does not contain
    any non-static data member whose type is const-qualified or a
    reference type

PS : if the answer could contain a reference to the standard it would be much appreciated

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

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

发布评论

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

评论(1

開玄 2025-02-04 15:26:42

按照标准合法?

是合法的吗?

该程序的行为不确定:

[basic.life]

在存储中创建一个新对象,该对象具有静态,线程或自动存储持续时间的完整对象,或者在其寿命结束之前用于占用的存储中占据了不确定的行为。

Is the following code legal according to the standard?

The behaviour of the program is undefined:

[basic.life]

Creating a new object within the storage that a const complete object with static, thread, or automatic storage duration occupies, or within the storage that such a const object used to occupy before its lifetime ended, results in undefined behavior.

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