39

I've heard that noexcept keyword is more like 'it should never throw an exception' rather than 'it doesn't'.

I don't think it's good to use noexcept keyword if I'm not sure it throws an exception or not, but noexcept keyword is sometimes related to the performance like in a move constructor.

So I tried to use noexcept qualifiers, but it gets harder if it has multiple statements in the definition and it becomes a kind of copy-and-paste thing.

template <class T>
void f(T&& t)
    noexcept(noexcept(statement_1) &&
             noexcept(statement_2) &&
             noexcept(statement_3) &&
             noexcept(statement_4) &&
             noexcept(statement_5))
{
    statement_1;
    statement_2;
    statement_3;
    statement_4;
    statement_5;
}

I think the compiler can figure out whether the definition of a function consists of non-throwing statements, so it will be easier to utilize noexcept if there's an expression like noexcept(auto), but it seems that there is no such thing in the standard.

Is there any way to simplify the noexcept expression?

4

1 に答える 1

18

現在、ありません。noexcept(auto)ただし、構文を提案するそのトピックに関する提案があり ます: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4473 "、Botond Ballo の "Trip Report: C++ Standards Meeting in Lenexa, May 2015" によるとhttps://botondballo.wordpress.com/2015/06/05/trip-report-c-standards-meeting-in-lenexa-may -2015/

今後の作業。提案の方向性は有望ですが、十分に具体化されていないか、1 つまたは複数の設計ポイントに特定の懸念があります。著者は、より肉付けされた、および/または述べられた懸念に対処する修正された提案を元に戻すことをお勧めします。

...

noexcept(auto) は、基本的に「この関数が呼び出す関数の noexcept 性から、この関数の noexcept 性を推測する」ことを意味します。戻り値の型推論と同様に、これには、関数を使用する各翻訳単位で使用できる関数の本体が必要です。例外仕様を型システムの一部にするという提案とともに、これは、関数の本体を変更すると関数の型が変更される可能性があることを意味することが提起されました (これも戻り値の型推論と同様です)。それ。

于 2015-06-09T09:10:48.960 に答える