放置在具有自动存储持续时间合法的常量变量上是新的吗?
以下代码是根据标准合法的吗?
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该程序的行为不确定:
The behaviour of the program is undefined: