HDembinski / HDembinski.github.io

Write-ups
MIT License
22 stars 5 forks source link

noexcept is not detected by the compiler. #4

Closed max0x7ba closed 4 years ago

max0x7ba commented 4 years ago

Thank you for the informative write-up.

With regards to "It is not necessary to mark every non-throwing function or method as noexcept, the compiler is able to detect simple cases." That would be great but it doesn't happen and the standard doesn't require that.

What happens in those examples in https://en.cppreference.com/w/cpp/language/noexcept

Is T(rvalue T) noexcept? true
Is T(lvalue T) noexcept? true

is that the compiler-implemented special member functions are noexcept (if possible) and that is why it prints true above.

Here is another example where a compiler should be able to deduce noexcept, but neither g++-8.4.0 nor clang++-8.0.0 deduce noexcept(true) for function f:

#include <iostream>

struct A {
    int f() const { return 1; }
    int g() const noexcept { return 2; }
};

int main() {
    A a;
    std::cout << noexcept(a.f()) << '\n';
    std::cout << noexcept(a.g()) << '\n';
}

Outputs:

0
1
HDembinski commented 4 years ago

Wow, thanks for the correction. I trusted the statement in https://en.cppreference.com/w/cpp/language/noexcept without testing in godbolt.

HDembinski commented 4 years ago

I will update the text ASAP.

HDembinski commented 4 years ago

implemented