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: Juan Cabrera <jjcp dot 91 at gmail dot com>
- To: ruslanngaripov at gmail dot com
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 6 Sep 2018 16:29:45 -0300
- Subject: Re: Question about a warning message
- References: <CAJRqx1xgfB2+G_5Fbhf=s-Wr5ACo9hwwB4ub4wV3KANaDaT_EQ@mail.gmail.com> <afb22a71-8740-79bd-5822-0515fac469eb@gmail.com>
Hello!
Thank you for your answers.
I'm aware that you can cast anything into an `enum class` but I
thought that casting a non valid enum-class value into an enum-class
was itself "wrong" or "unedfined behavior" or something like that :D.
I just checked and it looks like clang does not emit a warning in this case.
I guess I'll have to add a return statement to that function to keep
the compiler from complaning :D
Regards,
El jue., 6 sept. 2018 a las 15:56, Ruslan Garipov
(<ruslanngaripov@gmail.com>) escribió:
>
> > Shouldn't the compiler assume that function `f` always returns
> > something for all valid inputs `t`?
>
> I believe no, because for some crazy reason I can call `f` in the
> following way[1]:
>
> f(static_cast<T>(123));
>
> Therefore, argument `t` is neither `T::A`, nor `T::B`.
>
> Moreover:
>
> > 9.3.4 List-initialization [dcl.init.list]
> > 3 List-initialization of an object or reference of type T is defined
> > as follows:
> > (3.8) Otherwise, if T is an enumeration with a fixed underlying type
> > (9.6), the initializer-list has a single element v , and the
> > initialization is direct-list-initialization, the object is
> > initialized with the value T(v) (7.6.1.3); if a narrowing conversion
> > is required to convert v to the underlying type of T , the program is
> > ill-formed.
>
> Thus (I believe with `-std=c++17`) we can "end up" with:
>
> T bar{123};
> int foo = f(bar);
>
> Once again, the `f` function gets neither `T::A`, nor `T::B`.
>
> [1] "7.6.1.9 Static cast [expr.static.cast]. 10 A value of integral or
> enumeration type can be explicitly converted to a complete enumeration
> type."
>
> On 9/6/2018 10:58 PM, 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;
> > }
> > }
> >
> > https://godbolt.org/z/IXC4Ff
> >
> > 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).
> >
> > Regards,
> > Juan.
> >