False positive warning: missing return statement and switch statements

Steven L. Zook SLZook@Qualstar.com
Tue Nov 15 22:27:00 GMT 2005


What happens when someone calls with:

toInt( (Number)3 );

C/C++ lets you cast any numeric to an enum.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Frans Englich
Sent: Tuesday, November 15, 2005 2:21 PM
To: gcc-help@gcc.gnu.org
Subject: False positive warning: missing return statement and switch
statements


Hello,

For the code below, when I invoke "g++ -Wall file.cpp" with GCC version
3.3.4, I get:

file.cpp: In function `int toInt(Number)':
file.cpp:20: warning: control reaches end of non-void function

------------------------------------------------------------------------
-
enum Number
{
        Zero,
        One,
        Two
};

int toInt(const Number num)
{
        switch(num)
        {
                case Zero:
                        return 0;
                case One:
                        return 1;
                case Two:
                        return 2;
        }
}

int main()
{
        Number num = Zero;
        return toInt(num);
}
------------------------------------------------------------------------
-

I don't understand why the warning is issued. Isn't it fair to assume
that the argument(const Number num) is correct since it is strongly
typed, by being an enumerator?


Cheers,

		Frans



More information about the Gcc-help mailing list