I saw the following sentence in cppreference.com.
Any user-defined destructor is
noexcept(true)
by default, unless the declaration specifies otherwise, or the destructor of any base or member isnoexcept(false)
.
To know whether Visual C++ 2015 confirm this or not, I tried the following example.
#include <iostream>
class Test1 { public: ~Test1() = default; };
class Test2 { public: ~Test2() { } };
class Test3 { public: Test3() { } ~Test3() { } };
int main()
{
std::cout << std::boolalpha;
std::cout << "Test1: " << noexcept(Test1().~Test1()) << std::endl;
std::cout << "Test2: " << noexcept(Test2().~Test2()) << std::endl;
std::cout << "Test3: " << noexcept(Test3().~Test3()) << std::endl;
}
Result:
C:\Users\***>cl test.cpp
....
/out:test.exe
test.obj
C:\Users\***>test
Test1: true
Test2: false
Test3: false
However, when I turn on /EHsc
switch which enables exceptions, the result is changed.
C:\Users\***>cl test.cpp /EHsc
...
/out:test.exe
test.obj
C:\Users\***>test
Test1: true
Test2: true
Test3: false
Why did it happen?