경고 C26440

함수는 'noexcept'로 선언할 수 있습니다.

C++ 핵심 지침 F.6: 함수가 throw되지 않을 수 있는 경우 선언합니다. noexcept

코드가 예외를 발생하도록 되어 있지 않은 경우 지정자를 사용하여 noexcept 표시해야 합니다. 이 주석은 클라이언트 코드 쪽에서 오류 처리를 간소화하는 데 도움이 되며 컴파일러가 더 많은 최적화를 수행할 수 있도록 합니다.

설명

  • 함수는 다음과 같은 경우 throw되지 않는 것으로 간주됩니다.
    • 명시적 throw 문이 없습니다.
    • 함수는 본문에서 호출(있는 경우) throw constexpr 할 가능성이 낮은 함수만 호출하거나, throw되지 않는 동작을 수반하는 예외 사양으로 표시된 함수(일부 비표준 사양 포함)를 호출합니다.
  • 비표준 및 오래된 지정자는 .와 같 throw() 거나 __declspec(nothrow) 동일 noexcept하지 않습니다.
  • 명시적 지정자 noexcept(false) 이며 noexcept(true) 적절하게 준수됩니다.
  • constexpr 표시된 함수는 예외를 발생시킬 수 없으며 분석되지 않습니다.
  • 규칙은 람다 식에도 적용됩니다.
  • 논리는 재귀 호출을 잠재적으로 throw되지 않는 것으로 간주하지 않습니다. 이 논리는 나중에 변경될 수 있습니다.

예시

소멸자를 제외한 모든 함수는 noexcept가 없기 때문에 경고합니다.

struct S
{
    S() {} // C26455, Default constructor may not throw. Declare it 'noexcept'
    ~S() {}

    S(S&& s) {/*impl*/} // C26439, This kind of function may not throw. Declare it 'noexcept' (f.6)
    S& operator=(S&& s) {/*impl*/} // C26439, This kind of function may not throw. Declare it 'noexcept' (f.6)

    S(const S& s) {/*impl*/} // C26440, This function can be declared 'noexcept'
    S& operator=(const S& s) {/*impl*/} // C26440, This function can be declared 'noexcept'
};

noexcept가 동일한 구조를 데코레이팅하면 모든 경고가 제거됩니다.

struct S
{
    S() noexcept {}
    ~S() {}

    S(S&& s) noexcept {/*impl*/}
    S& operator=(S&& s) noexcept {/*impl*/}

    S(const S& s) noexcept {/*impl*/}
    S& operator=(const S& s) noexcept {/*impl*/}
};