noexcept 究竟包含什么构造函数?
What does noexcept exactly encompass for constructors?
根据 C++ 标准,class 构造函数上的 noexcept
noexcept-specification 究竟适用于什么?
- 函数体?
- 在可选的ctor-initializer中初始化成员?
- 在可选的 mem-initializers?
中初始化基础 classes
- 在可选的 mem-initializers?
中初始化 class 个成员
- 复合语句?
- 函数尝试块?
- 对象基的初始化class没有在ctor-initializer?
中初始化
- 初始化对象 class 成员未在 ctor-initializer?
中初始化
- 还有什么?
换句话说,noexcept
noexcept-specification 包含以上哪些内容(即在抛出异常时触发 std::terminate()
if noexcept(true)
)?
请提供对标准的引用。也欢迎对构造函数使用 noexcept
的任何注意事项提供提示。谢谢!
In other words, which of the above are encompassed by the noexcept
noexcept-specification...?
异常规范(noexcept
和动态异常规范)涉及基类 的构造、成员的构造和初始化以及构造函数主体中的代码。 基本上,在构造对象时执行的所有函数 - 这是有道理的,因为异常规范与对象的构造函数相关联,因此它应该涵盖构造对象期间执行的代码目的;如果构造的任何部分不在此范围内,那将是违反直觉的。
支持标准引号...
如果在构造过程中抛出异常(并且可能未处理)怎么办?
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]).
"outermost block of a function"是什么意思?函数体。1
上面的 exception specification 包括 noexcept-specification.
如何在隐式声明的构造函数上确定隐式声明的异常规范?
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"outermost block of a function"是什么意思?有人评论关注block的定义一个功能。该标准没有正式定义 函数块 。短语 block of a function 仅在 Exception Handling [except] 中使用。该短语早在 C++98 就包含在标准中。
为了进一步澄清这一点,我们需要寻找替代来源并得出一些合理的结论。
function body - the outermost block of a function. See also: try-block, function definition. TC++PL 2.7, 13.
并且从 [dcl.fct.def.general]/1 开始 函数体 的语法覆盖了 ctor-initializer 和 compound-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...
同样重要的是要记住,异常规范与函数相关联,而不是通用代码块(作用域块等)。
鉴于异常处理子句和 Stroustrup FAQ 中短语的存在时间,函数块 与 函数体,该标准可能会更新例外条款中使用的语言。
一些经验证据,给定下面的代码,用于构建 a1
、a2
和 a3
(当其他的被注释掉时),结果是 std::terminate
被召唤。结果适用于 g++, clang and MSVC.
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;
}
如果您考虑一个 noexcept
构造函数,其定义在当前翻译单元中不可用(例如,构造函数实现驻留在共享库中),您可能可以自己回答这个问题。
如果 noexcept
没有应用到整个构造函数,构造函数仍然会以某种方式发出异常,从而违背 noexcept
.
的目的
在这种情况下,noexcept
可以正常工作的唯一方法是将其应用于整个构造函数,包括基础 class 和成员初始化。
根据 C++ 标准,class 构造函数上的 noexcept
noexcept-specification 究竟适用于什么?
- 函数体?
- 在可选的ctor-initializer中初始化成员?
- 在可选的 mem-initializers? 中初始化基础 classes
- 在可选的 mem-initializers? 中初始化 class 个成员
- 复合语句?
- 函数尝试块?
- 在可选的ctor-initializer中初始化成员?
- 对象基的初始化class没有在ctor-initializer? 中初始化
- 初始化对象 class 成员未在 ctor-initializer? 中初始化
- 还有什么?
换句话说,noexcept
noexcept-specification 包含以上哪些内容(即在抛出异常时触发 std::terminate()
if noexcept(true)
)?
请提供对标准的引用。也欢迎对构造函数使用 noexcept
的任何注意事项提供提示。谢谢!
In other words, which of the above are encompassed by the
noexcept
noexcept-specification...?
异常规范(noexcept
和动态异常规范)涉及基类 的构造、成员的构造和初始化以及构造函数主体中的代码。 基本上,在构造对象时执行的所有函数 - 这是有道理的,因为异常规范与对象的构造函数相关联,因此它应该涵盖构造对象期间执行的代码目的;如果构造的任何部分不在此范围内,那将是违反直觉的。
支持标准引号...
如果在构造过程中抛出异常(并且可能未处理)怎么办?
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 allowE
, 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]).
"outermost block of a function"是什么意思?函数体。1
上面的 exception specification 包括 noexcept-specification.
如何在隐式声明的构造函数上确定隐式声明的异常规范?
An implicitly-declared special member function
f
of some classX
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, andif
X
is non-abstract ([class.abstract]), forX
'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"outermost block of a function"是什么意思?有人评论关注block的定义一个功能。该标准没有正式定义 函数块 。短语 block of a function 仅在 Exception Handling [except] 中使用。该短语早在 C++98 就包含在标准中。
为了进一步澄清这一点,我们需要寻找替代来源并得出一些合理的结论。
function body - the outermost block of a function. See also: try-block, function definition. TC++PL 2.7, 13.
并且从 [dcl.fct.def.general]/1 开始 函数体 的语法覆盖了 ctor-initializer 和 compound-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...
同样重要的是要记住,异常规范与函数相关联,而不是通用代码块(作用域块等)。
鉴于异常处理子句和 Stroustrup FAQ 中短语的存在时间,函数块 与 函数体,该标准可能会更新例外条款中使用的语言。
一些经验证据,给定下面的代码,用于构建 a1
、a2
和 a3
(当其他的被注释掉时),结果是 std::terminate
被召唤。结果适用于 g++, clang and MSVC.
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;
}
如果您考虑一个 noexcept
构造函数,其定义在当前翻译单元中不可用(例如,构造函数实现驻留在共享库中),您可能可以自己回答这个问题。
如果 noexcept
没有应用到整个构造函数,构造函数仍然会以某种方式发出异常,从而违背 noexcept
.
在这种情况下,noexcept
可以正常工作的唯一方法是将其应用于整个构造函数,包括基础 class 和成员初始化。