The new C++17 [[fallthrough]] "attribute"
Here I wanted to talk briefly about this new C++17 attribute:
[[fallthrough]];
I wanted that attribute like 30 years ago but the compiler programmers were definitely not ready to allow us to have safe coding practices...
Back then, any potential error was a beautiful feature. I'm really glad that some people pushed the idea that having tons of warnings about very possibly invalid code is a good thing long term. I am fixing some of my bugs just by compiling my code with newer versions of the compiler!
Now... I ran in a few problems with that new attribute. The fact is that it's a very special attribute. In fact, it is what is now called a statement attribute. What that means is that it has to be on a line of its own and the line has to end with a semi-colon. This is not like the other attributes such as the [[noreturn]]. So far attributes were mainly qualifiers. So having one which is a statement surprised me.
That is, it took me some time to figure out that I had to include the semi-colon for things to work properly.
Also, if you have old code that you want to fix (As in where you want to add the C++17 attribute and not keep your good old /* FALLTHROUGH */ comments, then you will want to add this command line option:
... -Wimplicit-fallthrough=5 ...
The 5 indicates that only the C++17 statement attribute is acceptable. Anything will be ignored.
I use that option and converted my code to pure C++17. I think that it makes more sense than supporting the comments now. More robust.
- Alexis Wilke's blog
- Login to post comments
Syndicate