PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] Why do I get `warning: control reaches end of non-void function` with switch-case?

 April 28, 2022     c++, c++11, clang++, function, warnings     No comments   

Issue

Consider code like this:

enum class Foo
{
    A, B
};

int deliverPizza(Foo foo)
{
    switch (foo) {
    case Foo::A:
        return 0;
    case Foo::B:
        return 1;
    }
}

int main()
{
    return deliverPizza(Foo::A);
}

Compiled with $ g++ -o main main.cpp -Wreturn-type:

main.cpp: In function ‘int deliverPizza(Foo)’:
main.cpp:14:1: warning: control reaches end of non-void function [-Wreturn-type]

Why is this? As far as I can see all the cases are handled within the switch. Is it that GCC doesn't understand that the cases return?

On the other hand adding default makes Clang to warn about Default label in switch which covers all enumeration values.


Solution

You can still run into UB when you construct the enum instance via a cast expression.

return deliverPizza(static_cast<Foo>(42));

This is allowed and happily compiles. I guess the warning is pedantic with respect to such a scenario.



Answered By - lubgr
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing