简体   繁体   中英

What is point in passing a function call expression into noexcept?

What is the difference between this:

bool foo(int){return 0;}
void bar()noexcept(foo){}

And

void bar()noexcept(foo(10)){}
  • In the first bar has the same exception specification as foo and the latter may throw so the two are equivalent to noexcept(false) . But what about the second version:

    noexcept(noexcept(foo(10))) ?

  • Are the two declarations identical?

What is the difference between this:

 bool foo(int){return 0;} void bar()noexcept(foo){}

And

void bar()noexcept(foo(10)){}

The difference isn't very important since both are ill-formed. But, let's look at why that is:

First is ill-formed because when foo is contextually convertd to bool , it will first implicitly convert to a pointer to function. Although the pointer is always going to point fo foo and thus the result is always true, such conversion is nevertheless not a constant expression and is thus not allowed in the noexcept specification.

Second is ill-formed because foo is not a constexpr function, so it may not be called in a noexcept specification.


But what about the second version noexcept(noexcept(foo(10))) ?

noexcept(foo(10)) is false if the function invoked by the expression foo(10) is potentially throwing, and true if it is noexcept. Thus, this achieves the "copying" of the exception specification. Nearly always this is done when the expression in question is used within the function:

void bar() noexcept(noexcept(foo(10))) {
    bool example = foo(10);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM