Question about a warning message

Ruslan Garipov ruslanngaripov@gmail.com
Thu Sep 6 18:56:00 GMT 2018


> 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.
> 



More information about the Gcc-help mailing list