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.
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';
}
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 that the compiler-implemented special member functions are
noexcept
(if possible) and that is why it printstrue
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 deducenoexcept(true)
for functionf
:Outputs: