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

此页面中列出的具名要求,是 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: truestd::is_destructible<A>::value: true
std::is_trivially_destructible<A>::value: true
std::is_nothrow_destructible<A>::value: truestd::is_destructible<B>::value: true
std::is_trivially_destructible<B>::value: false
std::is_nothrow_destructible<B>::value: truestd::is_destructible<C>::value: true
std::is_trivially_destructible<C>::value: false
std::is_nothrow_destructible<C>::value: true0x61fe80  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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/597890.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PythonStudio=vb7国人写的python可视化窗体设计器IDE,可以替代pyqt designer等设计器了

【免费】PythonStudio-1.1.5-x86最新版国人开发的python界面ide&#xff0c;可以制作窗体资源-CSDN文库https://download.csdn.net/download/xiaoyao961/88688447 【免费】PythonStudio-1.1.5-x64-Setup.exe国人开发的python界面ide&#xff0c;可以制作窗体资源-CSDN文库https…

SysML:系统建模语言在复杂系统设计中的应用

一、引言 SysML&#xff0c;即系统建模语言&#xff0c;是一种用于描述和分析复杂系统的标准化建模方法。它的发展起源于对软件开发过程中需求的不断演变和复杂化的认识。SysML的重要性在于它提供了一种统一的建模语言&#xff0c;能够有效地捕捉和表达系统的不同方面&#xf…

VM中安装Linux以及Win系统

目录 准备条件 安装RHEL9.3 步骤一&#xff1a;按照图片进行操作 步骤二&#xff1a;选择配置方式 步骤三&#xff1a;选择虚拟芯片 步骤四&#xff1a;安装镜像 步骤五&#xff1a;选择操作系统 步骤六&#xff1a;名字以及存储位置 步骤七&#xff1a;配置虚拟机参数…

Unity获取相机渲染范围内的所有物体

1.获取所有相机渲染的物体&#xff08;包括子物体&#xff09;。 using System.Collections.Generic; using UnityEngine;public class RenderedObjects : MonoBehaviour {public Camera camera;private void Start(){if (camera null){camera Camera.main;}}private void …

js object 去重

测试&#xff1a;数据 let arr [ { a: 1, b: 2 }, { b: 3, a: 4 }, { b: 2, a: 1 } ] 结果 function unique(arr) {const newArr [...arr]for (let i 0; i < newArr.length; i) {for (let j i 1; j < newArr.length; j) {if (this.duibi(newArr[i], newArr[j])) {ne…

帕金森病是否存在性别差异?

帕金森病是否存在性别差异是一个值得探讨的问题。从现有的研究和临床观察来看&#xff0c;帕金森病对男性和女性在某些方面确实存在差异。 首先&#xff0c;从发病率上看&#xff0c;帕金森病对男性的影响是女性的两倍。这意味着在相同的时间和地域背景下&#xff0c;男性患上…

秋招复习之栈与队列

前言 1 栈 「栈 stack」是一种遵循先入后出逻辑的线性数据结构。 我们可以将栈类比为桌面上的一摞盘子&#xff0c;如果想取出底部的盘子&#xff0c;则需要先将上面的盘子依次移走。我们将盘子替换为各种类型的元素&#xff08;如整数、字符、对象等&#xff09;&#xff0c…

LLM、AGI、多模态AI 篇三:微调模型

文章目录 系列LLM的几个应用层次Lora技术指令设计构建高质量的数据微调步骤系列 LLM、AGI、多模态AI 篇一:开源大语言模型简记 LLM、AGI、多模态AI 篇二:Prompt编写技巧 LLM、AGI、多模态AI 篇三:微调模型 LLM的几个应用层次 AI 端到端应用。是直接面向最终用户的应用程序…

how2heap-2.23-04-unsorted_bin_leak

#include<stdio.h> #include<malloc.h>int main() {char* a malloc(0x88);char* b malloc(0x8);free(a);long* c malloc(0x88);printf("%lx , %lx\n",c[0],c[1]);return 0; }unsorted bin leak原理&#xff1a;将chunk从unsorted bin申请回来时&#…

服务器磁盘挂载及格式化

一边学习,一边总结,一边分享! 写在前面 最近一直折腾组装的电脑,来回折腾了很久关于我花费六千多组了台window+Linux主机,目前基本是可以使用了。对于Windows主机配置基本是没问题,一直在使用,以及桌面化软件,都可以自己安装,只是说这台主机有些软件可能一时半会安装…

七款人体感应报警器电路图

人体感应报警器电路图&#xff08;一&#xff09; 人体发出的红外线波长在9&#xff5e;10um之间&#xff0c;属远红外线区。我们利用热释电红外传感器及信号处理集成电路&#xff0c;组装成一个人体红外线感应开关电路报警器&#xff0c;它能依靠人体发出的微量红外线进行开关…

阿里云服务器ECS实例规格如何选择?根据使用场景选择

阿里云服务器配置怎么选择合适&#xff1f;CPU内存、公网带宽和ECS实例规格怎么选择合适&#xff1f;阿里云服务器网aliyunfuwuqi.com建议根据实际使用场景选择&#xff0c;例如企业网站后台、自建数据库、企业OA、ERP等办公系统、线下IDC直接映射、高性能计算和大游戏并发&…

c++跨平台ui

fltk https://gitee.com/mirrors_fltk/fltk.git codeblock中有fltk项目开发模板,可以快速构建项目 wxwidget https://gitee.com/sofu456/wxWidgets.git git submodule update --init --recursive 打开demo和sample set(wxBUILD_SAMPLES ALL) set(wxBUILD_DEMOS ON) build/…

【springboot+vue项目(十一)】springboot整合EasyExcel

EasyExcel是阿里巴巴开源的一个Java库&#xff0c;用于操作Excel文件。它提供了简单易用的API&#xff0c;可以读取、写入和转换Excel文件&#xff0c;支持大量数据的导入和导出操作。 一、添加依赖&#xff08;版本3.2&#xff09; <!--easyexcel操作excel--> <depe…

Hadoop入门学习笔记——八、数据分析综合案例

视频课程地址&#xff1a;https://www.bilibili.com/video/BV1WY4y197g7 课程资料链接&#xff1a;https://pan.baidu.com/s/15KpnWeKpvExpKmOC8xjmtQ?pwd5ay8 Hadoop入门学习笔记&#xff08;汇总&#xff09; 目录 八、数据分析综合案例8.1. 需求分析8.1.1. 背景介绍8.1.2…

ocrmypdf_pdf识别

安装 安装说明 https://ocrmypdf.readthedocs.io/en/latest/installation.html#native-windows提到需要的软件&#xff1a; Python 3.7 (64-bit) or later Tesseract 4.0 or later Ghostscript 9.50 or later 安装 ocrmypdf pip install ocrmypdf 添加语言包 https://oc…

一篇文章带你了解基于 Jenkins 流水线方式部署的好处

在软件开发过程中&#xff0c;部署是将代码从开发环境转移到生产环境的关键步骤。传统的部署方式可能涉及多个手动步骤和容易出错的过程。然而&#xff0c;基于 Jenkins 流水线方式部署可以带来许多好处&#xff0c;包括提高效率、一致性和可靠性。本文将探讨基于 Jenkins 流水…

Flume基础知识(六):Flume实战之实时监控目录下的多个追加文件

Exec source 适用于监控一个实时追加的文件&#xff0c;不能实现断点续传&#xff1b;Spooldir Source 适合用于同步新文件&#xff0c;但不适合对实时追加日志的文件进行监听并同步&#xff1b;而 Taildir Source 适合用于监听多个实时追加的文件&#xff0c;并且能够实现断点…

C++高阶:元编程(Metaprogramming)--入门篇

模板元编程&#xff08;Template Meta programming&#xff0c;TMP&#xff09; 就是面向模板编程&#xff0c;把计算过程从运行时提前到编译期&#xff0c;提升性能&#xff1b; 区别于泛型编程&#xff08;利用模板实现“安全的宏”&#xff09; 应用场景&#xff1a; 编译期…

delphi中自定义自己的定时器

最近用上了rpt工具&#xff0c;但是用rpt自带的工具执行起一些定时任务不方便&#xff0c;有些功能不能自主&#xff0c;于是我在delphi中用定时器制作了自己的定时执行程序。 1、首先在窗体中放一个timer对象 2、在窗体的formcreate事件中加入以下关键代码&#xff0c;让定时器…