C++ 具名要求-基本概念-指定该对象可以析构

发布时间:2024年01月05日

此页面中列出的具名要求,是 C++ 标准的规范性文本中使用的具名要求,用于定义标准库的期待。

某些具名要求在 C++20 中正在以概念语言特性进行形式化。在那之前,确保以满足这些要求的模板实参实例化标准库模板是程序员的重担。若不这么做,则可能导致非常复杂的编译器诊断。

指定该对象可以析构

指定该类型的实例可被析构。

要求

以下情况下,类型 T 满足可析构 (Destructible)

给定

  • T 类型的表达式 u

则下列表达式必须合法且拥有其指定的效果

表达式后条件
u.~T()u 所拥有的全部资源都得到回收,不抛异常。

注解

在对象生存期结束(例如在离开作用域时或由于 delete 表达式)时隐式调用析构函数。如类型要求表中所示的显式析构函数调用是罕见的。

拜伪析构函数调用所赐,所有标量类型都满足可析构 (Destructible) 的要求,而数组类型和引用类型则不满足。注意 std::is_destructible 允许数组与引用类型。

调用示例

#include <iostream>
#include <type_traits>

//编译器生成默认构造函数
struct A
{
};

struct B
{
    std::string str; // 成员拥有非平凡默认构造函数
};

struct C
{
    std::string str; // 成员拥有非平凡默认构造函数
    C() throw (int) //构造函数抛异常
    {
    }
};

struct MyClass
{
    int ma;
    int mb;

    ~MyClass()
    {
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__ << std::endl;
    }

    MyClass(): ma(101), mb(102)
    {
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }

    MyClass(int a, int b): ma(a), mb(b)
    {
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }

    MyClass(const MyClass &obj)
    {
        this->ma = obj.ma;
        this->mb = obj.mb;
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }

    MyClass(MyClass &&obj)
    {
        this->ma = obj.ma;
        this->mb = obj.mb;
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
    }

    MyClass & operator =(MyClass &&obj)
    {
        this->ma = obj.ma;
        this->mb = obj.mb;
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
        return *this;
    }

    MyClass & operator =(const MyClass &obj)
    {
        this->ma = obj.ma;
        this->mb = obj.mb;
        std::cout << this << "  " << __FUNCTION__ << " " << __LINE__
                  << " a:" << ma << " b:" << mb
                  << std::endl;
        return *this;
    }
};

int main()
{
    std::cout << std::boolalpha;

    std::cout << "std::is_destructible<int>::value: "
              << std::is_destructible<int>::value << std::endl;
    std::cout << "std::is_trivially_destructible<int>::value: "
              << std::is_trivially_destructible<int>::value << std::endl;
    std::cout << "std::is_nothrow_destructible<int>::value: "
              << std::is_nothrow_destructible<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_destructible<A>::value: "
              << std::is_destructible<A>::value << std::endl;
    std::cout << "std::is_trivially_destructible<A>::value: "
              << std::is_trivially_destructible<A>::value << std::endl;
    std::cout << "std::is_nothrow_destructible<A>::value: "
              << std::is_nothrow_destructible<A>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_destructible<B>::value: "
              << std::is_destructible<B>::value << std::endl;
    std::cout << "std::is_trivially_destructible<B>::value: "
              << std::is_trivially_destructible<B>::value << std::endl;
    std::cout << "std::is_nothrow_destructible<B>::value: "
              << std::is_nothrow_destructible<B>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_destructible<C>::value: "
              << std::is_destructible<C>::value << std::endl;
    std::cout << "std::is_trivially_destructible<C>::value: "
              << std::is_trivially_destructible<C>::value << std::endl;
    std::cout << "std::is_nothrow_destructible<C>::value: "
              << std::is_nothrow_destructible<C>::value << std::endl;
    std::cout << std::endl;

    {
        MyClass myClass1 = {101, 102};
        //t = rv T& t 若 t 与 rv 不指代同一对象,则 t 的值等价于 rv 在赋值前的值。rv 的新值未指定
        MyClass() = myClass1;

        //u.~T() u 所拥有的全部资源都得到回收,不抛异常。
        myClass1.~MyClass();
    }
    return 0;
}

输出

std::is_destructible<int>::value: true
std::is_trivially_destructible<int>::value: true
std::is_nothrow_destructible<int>::value: true

std::is_destructible<A>::value: true
std::is_trivially_destructible<A>::value: true
std::is_nothrow_destructible<A>::value: true

std::is_destructible<B>::value: true
std::is_trivially_destructible<B>::value: false
std::is_nothrow_destructible<B>::value: true

std::is_destructible<C>::value: true
std::is_trivially_destructible<C>::value: false
std::is_nothrow_destructible<C>::value: true

0x61fe80  MyClass 41 a:101 b:102
0x61fe88  MyClass 34 a:101 b:102
0x61fe88  operator= 78 a:101 b:102
0x61fe88  ~MyClass 29
0x61fe80  ~MyClass 29
0x61fe80  ~MyClass 29

文章来源:https://blog.csdn.net/qq_40788199/article/details/135328151
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。