songyuanyao songyuanyao - 1 year ago 64
C++ Question

Does the standard specify whether a compiling error or warning should be generated?

I answered a question, in which I said for a narrowing conversion an error should be triggered. Because the standard says:

[ Note: As indicated above, such conversions are not allowed at the top level in list-initializations. — end note ]

I think the description of "not allowed" means the compiling should fail.

But another user disagreed, because from here just says "the program is ill-formed", and the standard won't require that compilation must fail.

if a narrowing conversion (see below) is required to convert the
element to T, the program is ill-formed.

So my question is: Does the standard specify whether an error or warning should be generated? Or for some cases the compiling should fail? From the aspect of a compiler, is it OK to make the program compile and just give some warnings?

BTW: Clang 4.0.0 and Gcc 7.0.0 behave differently.

float a {1.e39}; // Error for both Clang and GCC
double d;
float a3{d}; // Error for Clang, warning for GCC

Answer Source

The standard doesn't use the terms "error" and "warning", it only talks about cases where the compiler must "issue a diagnostic".

In your example, if the program is "ill-formed", the compiler is required to tell you that somehow - issue a diagnostic.

After that, it can do anything it likes - including compiling and running the program anyway. The standard only specifies what happens for conforming code, everything else is undefined. And then, as we know, anything can happen.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download