Function-try-block and noexcept

For the following code

struct X { int x; X() noexcept try : x(0) { } catch(...) { } }; 

Visual studio 14 CTP issues a warning

warning C4297: "X :: X": it is assumed that the function does not throw an exception, but does

Note: __declspec (nothrow), throw (), noexcept (true) or noexcept was specified in the function

Is this a misuse of noexcept ? Or is this a bug in the Microsoft compiler?

+8
c ++ c ++ 11 noexcept visual-c ++ c ++ 14
source share
1 answer

Or is this a bug in the Microsoft compiler?

Not really.

A so-called try-block function block like this cannot prevent an exception from outside. Keep in mind that an object is never completely constructed, since the constructor cannot complete execution. catch -block should throw something else or the current exception will be thrown ([except.handle] / 15):

The exception that is currently thrown is returned if control ends with the constructor's try-block handler or destructor.

Therefore, the compiler infers that the constructor can indeed throw.

 struct X { int x; X() noexcept : x(0) { try { // Code that may actually throw } catch(...) { } } }; 

Compile without warning.

+11
source share

All Articles