草庐IT

c++ - noexcept 究竟包含什么构造函数?

coder 2023-11-14 原文

根据 C++ 标准,类构造函数上的 noexcept noexcept-specification 究竟适用于什么?

  1. 函数体
    1. 在可选的 ctor-initializer 中初始化成员?
      1. 在可选的mem-initializer中初始化基类?
      2. 在可选的mem-initializer中初始化类成员?
    2. 复合语句
    3. 函数尝试 block
  2. 未在 ctor-initializer 中初始化对象基类的初始化?
  3. 未在 ctor-initializer 中初始化对象类成员的初始化?
  4. 还有什么吗?

换句话说,noexcept noexcept-specification 包含以上哪些内容(即触发 std::terminate() 时如果 noexcept(true))?

抛出异常

请提供对标准的引用。也欢迎对构造函数使用 noexcept 的任何注意事项提供提示。谢谢!

最佳答案

In other words, which of the above are encompassed by the noexcept noexcept-specification...?

异常规范(noexcept 和动态异常规范)涉及基类的构造、成员的构造和初始化以及构造函数主体中的代码。 基本上,在对象构造过程中执行的所有函数 - 这是有道理的,因为异常规范与对象的构造函数相关联,因此它应该涵盖在对象构造过程中执行的代码;如果构造的任何部分未包含在其中,那将是违反直觉的。

支持标准引号...

如果在构造过程中抛出异常(并且可能未处理)怎么办?

[except.spec]/9

Whenever an exception of type E is thrown and the search for a handler ([except.handle]) encounters the outermost block of a function with an exception specification that does not allow E, then,

  • if the function definition has a dynamic-exception-specification, the function std::unexpected() is called ([except.unexpected]),
  • otherwise, the function std::terminate() is called ([except.terminate]).

“函数的最外层 block ”是什么意思? 函数体。1

exception specification以上包括 noexcept-specification

如何根据隐式声明的构造函数确定隐式声明的异常规范?

[except.spec]/15

An implicitly-declared special member function f of some class X is considered to have an implicit exception specification that consists of all the members from the following sets:

  • if f is a constructor,

    • the sets of potential exceptions of the constructor invocations

      • for X's non-variant non-static data members,
      • for X's direct base classes, and
      • if X is non-abstract ([class.abstract]), for X's virtual base classes,

        (including default argument expressions used in such invocations) as selected by overload resolution for the implicit definition of f ([class.ctor])...

    • the sets of potential exceptions of the initialization of non-static data members from brace-or-equal-initializers that are not ignored ([class.base.init]);

这对编译器将使用什么来确定(并因此考虑涵盖)异常规范提供了非常有用的说明。


1“函数的最外层 block ”是什么意思?有人评论关注函数 block 的定义。该标准没有对函数 block 的正式定义。短语block of a function 仅用于Exception Handling [except] .该短语早在 C++98 就包含在标准中。

为了进一步澄清这一点,我们需要寻找替代来源并得出一些合理的结论。

来自 Stroustrup C++ glossary ;

function body - the outermost block of a function. See also: try-block, function definition. TC++PL 2.7, 13.

来自[dcl.fct.def.general]/1 function-body 的语法,它覆盖了 ctor-initializercompound-statement 以及 function-try-block;

Function definitions have the form;

...

  function-body:
    ctor-initializeropt compound-statement
    function-try-block

...

Any informal reference to the body of a function should be interpreted as a reference to the non-terminal function-body...

同样重要的是要记住,异常规范与函数相关联,而不是通用代码块(作用域 block 等)。

考虑到异常处理子句和 Stroustrup FAQ 中短语的存在时间,函数 block 函数体 相同,标准可以可能会更新异常(exception)条款中使用的语言。


一些经验证据,给出下面的代码,用于构建a1a2a3(当其他的被注释掉时) ,导致 std::terminate 被调用。结果适用于 g++, clangMSVC .

struct Thrower { Thrower() { std::cout << "throwing..." << std::endl; throw 42; } };

struct AsMember { Thrower t_; AsMember() noexcept : t_{} { std::cout << "ctor" << std::endl; } };

struct AsBase : Thrower { AsBase() noexcept { std::cout << "ctor" << std::endl; } };

struct AsNSDMI { Thrower t_ {}; AsNSDMI() noexcept { std::cout << "ctor" << std::endl; } };

int main()
{
    std::set_terminate([](){ std::cout << "terminating..." << std::endl; });
    try {
        //AsMember a1{};
        //AsBase a2{};
        AsNSDMI a3{};
    }
    catch (...) { std::cout << "caught..." << std::endl; }
    return 0;
}

关于c++ - noexcept 究竟包含什么构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295738/

有关c++ - noexcept 究竟包含什么构造函数?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  8. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  9. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  10. ruby - Infinity 和 NaN 的类型是什么? - 2

    我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串

随机推荐