只谈C++11新特性 - Noexcept 说明符

C++11新特性总结 文章目录类型说明符空指针nullptr基于范围的 for 循环初始化列表构造函数 类型说明符 auto:用于从初始化表达式中推断出变量的数据类型。auto定义的变量必须有初始值。 auto a = 10;//typeid(a).name()==int auto b ;// err decltype:用于选择并返回操作数的数据类型。编译时类型推导,以一个普通表达式为参数,返回该表达式类型,并... 阅读详情

Noexcept 说明符

背景

在 C++11 之前,C++ 提供了 throw 规范用于声明一个函数可能抛出的异常类型,例如:

void func() throw(int, double); // 表示 func 可能抛出 int 或 double 类型的异常

但是,这种规范有以下几个主要问题:

  1. 运行时检查:throw 规范在函数运行时进行异常类型的检查。如果函数抛出的异常类型不在规范范围内,程序会调用 std::unexpected,通常会导致程序崩溃。
    • std::unexpected 是 C++98 中的机制,当一个函数抛出了未列入 throw 规范的异常时,会调用该函数。
    • 默认情况下,std::unexpected 会调用 std::terminate,从而终止程序。
    • 开发者可以通过 std::set_unexpected 自定义处理逻辑,但这种机制的实际使用价值较低,尤其在性能敏感场景中。
  2. 性能开销:由于需要在运行时进行检查,这会引入额外的性能开销。
  3. 限制性不足:throw 规范的可读性和实用性较低,开发者往往选择忽略这种用法。

此外,std::terminate 是 C++ 中的一个核心函数,用于在无法继续执行的异常场景下终止程序。

  • 触发场景:
    1. 无捕获的异常。
    2. 栈展开时发生了新的异常(双重异常)。
    3. 手动调用 std::terminate。
  • 自定义行为:开发者可以通过 std::set_terminate 设置终止行为的自定义逻辑,但默认行为是直接退出程序。

由于上述问题,C++11 引入了更高效的 noexcept 关键字来替代 throw 规范。


noexcept 特性简介

noexcept 是一种用于声明函数不会抛出异常的关键字,它相比 throw 规范提供了以下改进:

  1. 编译期检查:noexcept 能够在编译期确定函数是否可以抛出异常,从而避免运行时检查带来的开销。
  2. 性能优化:编译器可以利用 noexcept 的信息对代码进行优化,例如在 std::move 和容器操作中。
  3. 语义清晰:noexcept 明确声明了函数的异常安全性,增强了代码的可读性和可维护性。

noexcept 的语法如下:

void func() noexcept; // 表示 func 不会抛出异常

此外,还可以使用 noexcept(expression) 形式,其中 expression 是一个布尔表达式,返回值为 true 时表示函数不会抛出异常。

void func() 
西门子PLC与汇川SV660P伺服速度模式配置实战指南 本文详细介绍了西门子S7-1200 PLC与汇川SV660P伺服驱动器在速度模式下的配置实战指南。从硬件接线、伺服参数配置到TIA Portal软件组态和PLC程序编写,提供了全面的操作步骤和实用技巧,帮助工程师快速解决伺服系统调试中的常见问题,确保系统稳定运行。 阅读详情

相关推荐

地心地固坐标系与北天东坐标系的转换

经过这一步旋转,x3轴和y3轴就处在过O3点的经度线所在的平面了,而且z3轴指向了东。将图3的坐标系旋转成图2的样子。这里需要多次旋转,每次以某个轴为对称轴进行旋转,对应的坐标变换参考。的原点在地心,Z轴指向北极点,X轴指向本初子午线与赤道的交点,Y轴指向符合右手定则,如下图所示。下面实现地心地固坐标系到北天东坐标系的转换,问题描述为:设一点。以y1轴为对称轴,顺时针旋转90°,得到图4的样子。,X轴指向北,Y轴指向天,Z轴指向东,如下图所示。因此,北天东坐标下的坐标为。点在图3坐标系下的坐标。

csdnqixiaoxin的博客 3556

Zemax实战:手把手教你设计一个40倍、NA0.65的显微物镜(附初始结构文件)

本文详细介绍了使用Zemax设计40倍、NA0.65显微物镜的完整流程,从初始结构搭建到优化验证,涵盖光学设计的关键技巧和实战经验。通过分阶段优化策略和公差分析,帮助工程师快速掌握高倍显微物镜的设计方法,提升光学系统性能。

weixin_42525369的博客 344

从‘裸纤’到成品:手把手拆解光纤阵列(FA)的装配、点胶与端面研磨全流程

本文详细拆解了光纤阵列(FA)从裸纤到成品的全流程,包括装配、点胶与端面研磨等关键工艺。通过实战经验和数据对比,揭示了5G基站、数据中心等场景中FA的精密制造要点与常见故障解决方案,为光通信器件生产提供专业指导。

weixin_42652674的博客 746

测试用例经典练习之微信发红包测试用例

微信发红包测试用例

ZCYaiCLX3344的博客 1万+

三菱CNC数据采集实战:从SDK安装到C#代码调试全流程(附完整资源包)

本文为C#开发者提供了三菱CNC数据采集的完整实战指南。详细介绍了从获取并安装核心A2协议SDK、配置Visual Studio开发环境,到理解COM接口文档、封装C#通信类,并构建具备多设备连接与定时采集功能的WinForms应用程序的全流程。文章重点分享了网络调试、异常处理及性能优化等关键实战经验,帮助开发者高效打通工业自动化数据采集链路。

oauth7security的博客 707

安卓直链APK安装拦截全解析:从Play Protect到厂商管控的应对指南

在移动应用分发领域,APK(Android Package)安装是开发者与用户交互的关键环节。其核心原理涉及安卓系统的安全沙箱机制与权限管理体系,旨在保护用户设备免受恶意软件侵害。这一机制的技术价值在于平衡了应用生态的开放性与安全性,但也在应用推广、企业内部分发等场景中带来了挑战。尤其在通过官网、广告等直链渠道分发APK时,常会遇到安装转化率骤降的问题。这背后主要受到Google Play Protect云端扫描、安卓系统安装权限模型演进(如Android 8.0+的安装源授权)以及设备制造商附加安全层(如

weixin_33894640的博客 468

混合编程实战:C#集成C++ PCL点云处理库,打造跨语言动态链接库

本文详细介绍了C#与C++混合编程实战,重点讲解如何通过动态链接库(DLL)集成C++ PCL点云处理库,实现跨语言协作。内容涵盖环境配置、PCL功能封装、C#调用DLL的完整方案,以及性能优化和错误处理等进阶技巧,帮助开发者高效处理大规模点云数据,提升工业软件开发效率。

weixin_42620913的博客 382

SAP Group Reporting入门指南(1)--开篇及系统环境搭建

文章介绍了SAP GR的发展历程、与BPC的对比优势,数据收集的多种方式(实时同步、外部导入等)以及合并整体流程,并推出《SAP Group Reporting入门指南》专栏。

bflyfish的博客 282

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

上一篇: 只谈C++11新特性 - 限定引用的成员函数
下一篇: 只谈C++11新特性 - char32_t 和 char16_t
魔法工坊
博客等级 码龄2年 377粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值