4

For this code:

enum class Colors { Red, Green, Blue };

int fun(Colors color)
{
    switch (color)
    {
        case Colors::Red: return 0;
        case Colors::Blue: return 1;
        case Colors::Green: return 2;
    }
}

My compiler threw this error at me:

warning: control reaches end of non-void function [-Wreturn-type]

I know it is undefined behavior to have no return statement in a function, but is it undefined behavior to not have a return statement for all control paths? Thanks in advance.

5
  • "no return statement in a function" is just a special case of "not have a return statement for all control paths"... Mar 8, 2014 at 11:29
  • In short, yes, it's UB. Mar 8, 2014 at 11:29
  • 1
    It's only UB if it happens. If your switch variable can never have an unhandled value, the function doesn't flow off the end, so it's not UB. Sep 10, 2016 at 14:16
  • 1
    g++ gurns about this, but interestingly, clang++ does not. IMHO there should always be an option for users who use their own functions properly and have no interest in being warned that some imaginary caller could cast an out-of-range value into an enum class and pass it in. Assuming proper enum class usage, this switch will always hit a valid case, and the end of the function won't be flowed off, meaning no UB is involved. Why does g++ insist that I must artificially insert a default: or return or abort() just to avoid being shouted at about this? Sep 10, 2016 at 15:09
  • 1
    Clang, if you handle all your enum (including non-class) cases, is silent, and if you don't - of course you get the Wswitch message, but then warning: control may reach end of non-void function [-Wreturn-type]. So Clang's Wreturn-type only fires in missing cases and even then says "may reach" (i.e. thanks to a stupid/malicious caller), not reaches. GCC could do one better by defaulting to warning about such callers - but allowing the warning to be disabled by competent ones. But it seems they don't want to be that nuanced ;-) gcc.gnu.org/bugzilla/show_bug.cgi?id=53479 Sep 10, 2016 at 15:17

3 Answers 3

0

In your case, the return value of fun is not void, so, yes, it's undefined behavior.

C++11 6.6.3 The return statement

[...] Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

1
  • 3
    It's only UB if it happens. If the switch variable can never have an unhandled value, the function doesn't flow off the end, so it's not UB. Sep 10, 2016 at 14:16
0

Reason is that you have called your fun function with incorrect value, not Red, Blue or Green. You can change your code to

int fun(Colors color)
{
    switch (color)
    {
        case Colors::Red: return 0;
        case Colors::Blue: return 1;
        case Colors::Green: return 2;
        default: return -1; // Unknown color here!
    }
}
-1

It's okay to not have a return statement in a function, if it returns void. However, for a function not returning void it's always undefined behavior to not have a valid return value from any execution path.

So in your case, you need a return with a value after the switch.

Note that you don't need a default case if you list all the possible cases in the switch statement.

1
  • 1
    The UB only exists if the non-returning path is taken. It shouldn't be with an enum class, and we should be allowed to tell the compiler to shut up and assume we don't use casting acrobatics to pass in an invalid value. Anyway, about your claim that a default isn't needed if all the cases are listed, then g++ disagrees and still prints its incessant, inapplicable warning: coliru.stacked-crooked.com/a/a7ac7406bda6c430 Sep 10, 2016 at 14:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.