Error: this statement may fall through [-Werror=implicit-fallthrough=]

You should add keyword break to each case statement, if you don't do that, the code will run from case which it matches condition and continue to meet the

break;

for example: If VDimension = 4, then the code will run from case 4 => continue to case 3 => contine to case 2 then break. It means that it will execute below commands:

offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
offset = offset + idx[2]*imageDims[0]*imageDims[1];
offset  = offset + idx[0] + idx[1]*imageDims[0];
break;
return offset;

I think your code should be:

/** Get memory offset for a given image index */
  unsigned int GetOffset(const IndexType & idx) const
  {
   const unsigned int * imageDims = m_ImageDataItem->m_Dimensions;

    unsigned int offset = 0;
    switch(VDimension)
    {
    case 4:
     offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
     break;
    case 3:
     offset = offset + idx[2]*imageDims[0]*imageDims[1];
     break;
    case 2:
     offset  = offset + idx[0] + idx[1]*imageDims[0];
     break;
    }

    return offset;
  }

Switch case statements will fall through by default. In the case of the shown program, if VDimension is 4 then all of

offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
offset = offset + idx[2]*imageDims[0]*imageDims[1];
offset  = offset + idx[0] + idx[1]*imageDims[0];

will be executed.

In some other languages, such as Pascal, only one case is executed and there is no concept of fall through. As such, programmers new to C++ may write fall through switches unintentionally.

If the fall through is unintentional, you need to add a break between each case to not fall through.

this statement may fall through

This warning notifies the programmer about the fall through. This warning option can be controlled with the GCC compiler switch -Wimplicit-fallthrough. It is not enabled by default and is not enabled by -Wall, but it is enabled by -Wextra.

Warnings become errors if the -Werror switch is used. -Werror is not enabled by default.

C++17 introduced [[fallthrough]] attribute, which can be used to explicitly document the fall through when it is intentional. The compiler should not warn if it is used:

        switch(VDimension)
        {
        case 4:
         offset = offset + idx[3]*imageDims[0]*imageDims[1]*imageDims[2];
         [[fallthrough]];
        case 3:
         offset = offset + idx[2]*imageDims[0]*imageDims[1];
         [[fallthrough]];
        case 2:
         offset = offset + idx[0] + idx[1]*imageDims[0];
         break;
        }

Prior to C++17, GCC provides a language extension attribute __attribute__ ((fallthrough)) for the same purpose.

The fall through can also be documented with a comment, and Wimplicit-fallthrough may detect such comment depending on what value was used with the switch. More details in the documentation of GCC.


If you're absolutely sure the warnings are inconsequential, remove the -Werror flag during compilation.

For projects which put the flag during configure stage, you can do

./configure --disable-werror

before running

make