Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ standard specify that for some cases the compiling should fail with an error?

I'm checking the standard about narrowing conversion, and I think 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 someone told me that 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
like image 329
songyuanyao Avatar asked Nov 25 '16 14:11

songyuanyao


People also ask

Does C code need to be compiled?

C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.

What happens when C program is compiled?

Whenever a C program file is compiled and executed, the compiler generates some files with the same name as that of the C program file but with different extensions.

How does GCC treat warning errors?

You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning.

What is GCC compiler error?

The name of the assembler program with gcc is just as . So the error message tells you, that running the assembler fails, because the assembler executable contains an illegal instruction. This might really be an hardware error, meaning that the executable of the assembler is broken.


3 Answers

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.

like image 117
Bo Persson Avatar answered Oct 13 '22 11:10

Bo Persson


The only requirement for an ill-formed program is that the compiler must "issue a diagnostic", where "diagnostic" has an implementation-defined meaning. Having done that, the compiler is free to continue to compile the code. That's the main hook for implementation-specific behavior.

like image 21
Pete Becker Avatar answered Oct 13 '22 09:10

Pete Becker


If a program not is ill-formed, the compiler must produce executable output. If the program contains no UB, the executable must behave as the abstract machine the standard describes states it will behave. If it does contain UB, the executable can do anything.

If the program is ill-formed with no diagnostic required, the compiler can do anything. It can produce an executable output, or not. That executable output can do anything at all. It could design a program that seems to match the intent of the code, for example.

Compilers are free to print diagnostics whenever they want.

Compilers are mandated to print diagnostics in some situations. "Most" ill-formed programs require a diagnostic. What a diagnostic is exactly is implementation defined. It has been noted that printing a single blank newline, or a space, is a valid diagnostic under the standard.

That would be considered a poor quality of implementation.

Once the diagnostic is printed when there is an ill-formed program that requires a diagnostic, the compiler is free to do anything. It can produce an executable that somewhat matches what you ask for, produce an executable that does anything it wants, or produce no executable.

The standard does not differentiate between warnings and errors.

An ill-formed program that requires a diagnostic that print a warning, then continue to compile, does not violate the standard.

An ill-formed program that requires a diagnostic that prints an error, then doesn't continue to compile, does not violate the standard.

An ill-formed program with no diagnistic required can print a diagnostic. It may choose to produce an executable or not. The executable could do something reasonable or not.

A well formed program can have the compiler issuing a diagnostic. This diagnostic could be described as a warning. It could also be described as an error, but the compiler must produce an executable, and the executable must do what the standard mandates.

like image 23
Yakk - Adam Nevraumont Avatar answered Oct 13 '22 09:10

Yakk - Adam Nevraumont