2

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 is noexcept(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?

ikh
  • 10,119
  • 1
  • 31
  • 70
  • 2
    This is unrelated, but just to not offend the eye to much, maybe you could write the unevaluated expression as `std::declval().~T()`? Otherwise it looks too much like instant undefined behaviour (even though it is technically correct). – Kerrek SB Jan 25 '16 at 10:37

0 Answers0