This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question about a warning message
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Josh dot Scoggins at parasoft dot com
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Fri, 7 Sep 2018 09:04:20 +0100
- Subject: Re: Question about a warning message
- References: <CAJRqx1xgfB2+G_5Fbhf=s-Wr5ACo9hwwB4ub4wV3KANaDaT_EQ@mail.gmail.com> <e2ada194-1a98-3acd-14a6-91595717f78f@parasoft.com>
On Thu, 6 Sep 2018 at 19:53, Joshua Scoggins <Josh.Scoggins@parasoft.com> wrote:
>
> Hello,
>
>
> On 09/06/2018 10:58 AM, Juan Cabrera wrote:
> > Hello,
> >
> > I'm getting a "control reaches end of non-void function" warning
> > message on the following code (tested with gcc version 7.3 and above):
> >
> > enum class T { A, B };
> >
> > int f(T t) {
> > switch (t) {
> > case T::A: return 10;
> > case T::B: return 20;
> > }
> > }
> >
> >
> > Shouldn't the compiler assume that function `f` always returns
> > something for all valid inputs `t`? (Given that the parameter `t` is
> > of type `T` which is an enum class and the siwtch statement covers all
> > the enum values).
> >
>
> No the compiler should not assume this as an enum class does not protect
> you from invalid input and casts (only implicit conversion is
> prevented). Here is an example:
>
> int performOperation() {
> return f(T::A) + f(T::B) + f(static_cast<T>(3));
> }
>
> The legal values for T are A and B which are 0 and 1 respectively.
No, it's an enum class, so all values of the underlying type are
valid. The underlying type here is 'int' so any value of int is a
valid value of T.
> Three
> is not a legal value for type T but it can be cast into it.
3 is a valid value.
I wish people understood enums.