使用指针显式调用指向对象的析构函数后,指针将持有哪种值? [英] What kind of value does a pointer hold after using it to explicitly call the pointed object's destructor?

查看:68
本文介绍了使用指针显式调用指向对象的析构函数后,指针将持有哪种值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 https://timsong-cpp.github.io/cppwp/basic.compound#3 :

指针类型的每个值都是以下之一:

Every value of pointer type is one of the following:

  • 指向对象或函数的指针(据说该指针指向该对象或函数),或者
  • 超出对象末尾的指针([expr.add]),或
  • 该类型的空指针值,或
  • 无效的指针值.

使用指针显式调用对象的析构函数后,指针具有这四种值中的哪一种?例子:

After using a pointer to explicit call an object's destructor, which of these four kinds of value does the pointer have? Example :

#include <vector>

struct foo {
    std::vector<int> m;
};

int main()
{
    auto f = new foo;
    f->~foo();
    // What is the value of `f` here?
}

我不认为它可以是指向对象或函数的指针.不再有指向的对象它不是函数指针.

I don't believe it can be a pointer to an object or function. There is no longer an object to point to and it is not a function pointer.

我不认为它可以是对象末尾的指针.没有任何类型的指针运算,也没有涉及数组.

I don't believe it can be a pointer past the end of an object. There wasn't any sort of pointer arithmetic and no array is involved.

我不认为它可以是空指针值,因为指针不是 nullptr .它仍然指向对象拥有的存储,您可以使用它执行放置 new .

I don't believe it can be a null pointer value since the pointer is not nullptr. It still points to the storage the object had, you could use it to perform placement new.

我不认为它可以是无效的指针值.无效的指针值与存储持续时间的结束而不是对象生存期相关联. "指针值在表示存储时变为无效到达其存储期限的末尾" .存储仍然有效.

I don't believe it can be an invalid pointer value. Invalid pointer values are associated with the end of storage duration, not object lifetime. "A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration". The storage is still valid.

在我看来,指针没有指针值.我哪里出问题了?

It seems to me like there is no pointer value the pointer could have. Where did I go wrong?

推荐答案

它是指向对象的指针,但是对象不在其生存期内.

It's the pointer to the object, but the object is just not within its lifetime.

[basic.compound],脚注42)中:

对于不在其生存期内的对象,这是它将占据或曾经占据的内存中的第一个字节.

For an object that is not within its lifetime, this is the first byte in memory that it will occupy or used to occupy.

这篇关于使用指针显式调用指向对象的析构函数后,指针将持有哪种值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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