Movatterモバイル変換


[0]ホーム

URL:


14 Exception handling[except]

14.5 Exception specifications[except.spec]

The predicate indicating whether a function cannot exit via an exceptionis called theexception specification of the function.
If the predicate is false,the function has apotentially-throwing exception specification,otherwise it has anon-throwing exception specification.
The exception specification is either defined implicitly,or defined explicitlyby using anoexcept-specifieras a suffix of afunction declarator.
In anoexcept-specifier, theconstant-expression,if supplied, shall be a contextually converted constant expressionof typebool ([expr.const]);that constant expression is the exception specification ofthe function type in which thenoexcept-specifier appears.
A( token that followsnoexcept is part of thenoexcept-specifier and does not commence aninitializer ([dcl.init]).
Thenoexcept-specifiernoexceptwithout aconstant-expressionisequivalent to thenoexcept-specifiernoexcept(true).
[Example 1: void f()noexcept(sizeof(char[2]));// error: narrowing conversion of value 2 to typeboolvoid g()noexcept(sizeof(char));// OK, conversion of value 1 to typebool is non-narrowing — end example]
If a declaration of a functiondoes not have anoexcept-specifier,the declaration has a potentially throwing exception specificationunless it is a destructor or a deallocation functionor is defaulted on its first declaration,in which cases the exception specificationis as specified belowand no other declaration for that functionshall have anoexcept-specifier.
In anexplicit instantiationanoexcept-specifier may be specified,but is not required.
If anoexcept-specifier is specifiedin an explicit instantiation,the exception specification shall be the same asthe exception specification of all other declarations of that function.
A diagnostic is required only if theexception specifications are not the samewithin a single translation unit.
If a virtual function has anon-throwing exception specification,all declarations, including the definition, of any functionthat overrides that virtual function in any derived classshall have a non-throwingexception specification,unless the overriding function is defined as deleted.
[Example 2: struct B{virtualvoid f()noexcept;virtualvoid g();virtualvoid h()noexcept=delete;};struct D: B{void f();// errorvoid g()noexcept;// OKvoid h()=delete;// OK};
The declaration ofD​::​fis ill-formed because ithas a potentially-throwing exception specification,whereasB​::​fhas a non-throwing exception specification.
— end example]
An expressionE ispotentially-throwing if
An implicitly-declared constructor for a classX,or a constructor without anoexcept-specifierthat is defaulted on its first declaration,has a potentially-throwing exception specificationif and only ifany of the following constructs is potentially-throwing:
  • the invocation of a constructor selected by overload resolutionin the implicit definition of the constructorfor classXto initialize a potentially constructed subobject, or
  • a subexpression of such an initialization,such as a default argument expression, or,
  • for a default constructor, a default member initializer.
[Note 1: 
Even though destructors for fully-constructed subobjectsare invoked when an exception is thrownduring the execution of a constructor ([except.ctor]),their exception specifications do not contributeto the exception specification of the constructor,because an exception thrown from such a destructorwould call the functionstd​::​terminaterather than escape the constructor ([except.throw],[except.terminate]).
— end note]
The exception specification for an implicitly-declared destructor,or a destructor without anoexcept-specifier,is potentially-throwing if and only ifany of the destructorsfor any of its potentially constructed subobjectshas a potentially-throwing exception specification orthe destructor is virtual and the destructor of any virtual base classhas a potentially-throwing exception specification.
The exception specification for an implicitly-declared assignment operator,or an assignment-operator without anoexcept-specifierthat is defaulted on its first declaration,is potentially-throwing if and only ifthe invocation of any assignment operatorin the implicit definition is potentially-throwing.
Adeallocation functionwith no explicitnoexcept-specifierhas a non-throwing exception specification.
The exception specification for a comparison operator function ([over.binary])without anoexcept-specifierthat is defaulted on its first declarationis potentially-throwing if and only ifany expressionin the implicit definition is potentially-throwing.
[Example 3: struct A{ A(int=(A(5),0))noexcept; A(const A&)noexcept; A(A&&)noexcept;~A();};struct B{ B()noexcept; B(const B&)=default;// implicit exception specification isnoexcept(true) B(B&&,int=(throw42,0))noexcept;~B()noexcept(false);};int n=7;struct D:public A,public B{int* p=newint[n];//D​::​D() potentially-throwing, as thenew operator may throwbad_alloc orbad_array_new_length//D​::​D(const D&) non-throwing//D​::​D(D&&) potentially-throwing, as the default argument forB's constructor may throw//D​::​~D() potentially-throwing};
Furthermore, ifA​::​~A()were virtual,the program would be ill-formed since a function that overrides a virtualfunction from a base classshall not have a potentially-throwing exception specificationif the base class function has a non-throwing exception specification.
— end example]
An exception specification is considered to beneeded when:
  • in an expression, the function is selected byoverload resolution ([over.match],[over.over]);
  • the function is odr-used ([basic.def.odr]);
  • the exception specification is compared to that of anotherdeclaration (e.g., an explicit specialization or an overriding virtualfunction);
  • the function is defined; or
  • the exception specification is needed for a defaultedfunction that calls the function.
    [Note 2: 
    A defaulted declaration does not require theexception specification of a base member function to be evaluateduntil the implicit exception specification of the derivedfunction is needed, but an explicitnoexcept-specifier needsthe implicit exception specification to compare against.
    — end note]
The exception specification of a defaultedfunction is evaluated as described above only when needed; similarly, thenoexcept-specifier of a specializationof a templated functionis instantiated only when needed.

[8]ページ先頭

©2009-2026 Movatter.jp