I was testing C++17 features on GCC compiler version 7.1.0.
This is related to the fallthrough
attribute and the following example (live example) is adapted from online CPP reference here
#include "iostream"
using namespace std;
int f(int n) {
switch (n) {
case 1:
case 2:
n = n + 20;
[[fallthrough]];
case 3: // no warning on fallthrough
n = n + 30;
case 4: // compiler may warn on fallthrough
[[fallthrough]]; // illformed, not before a case label
//n = n + 40; //commented out to test if compiler will warn.
}
return n;
}
int main()
{
cout << f(1) << endl;
cout << f(2) << endl;
cout << f(3) << endl;
cout << f(4) << endl;
return 0;
}
The last [[fallthrough]]
(for case 4:
) is ill-formed.
The question on "What is the C++ compiler required to do with ill-formed programs according to the Standard?" here has the top answer stating that:
So to sum it up: if an ill-formed program contains a diagnosable violation for which the Standard does not explicitly specify "no diagnostic required", conforming implementations should emit a diagnostic.
So, I looked up the standard (N4713) to see if it stated that there was no diagnostic was required for this issue. I was not able to find any such statement.
Interestingly, after all this, when I added the following statement after the last [[fallthrough]]
n = n + 40;
the compiler warns (live example):
warning: attribute 'fallthrough' not preceding a case label or default label
So, two questions here:
- Has the compiler missed out on emitting a diagnostic, or am I missing something here?
- If it is a compiler issue, is it serious enough to be reported?
[[fallthrough]]
at the end) as an ill-formed construction. I'd say this is not afallthrough
question, but rather handling of an ill-formed construct. One can copy paste ill-formed example from standard into gcc and it compiles cleanly: godbolt.org/z/fftSM-. – Cardwell