This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/62096] unexpected warning overflow in implicit constant conversion


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62096

--- Comment #4 from Travis Vitek <travis.vitek at roguewave dot com> ---
I believe the underlying type of the unscoped enumeration `E' should be `int'.
According to 7.2 paragraph 7, the underlying type is

  .. implementation-defined which integral type is used
  as the underlying type except that the underlying
  type shall not be larger than int unless the value
  of an enumerator cannot fit in an int or unsigned int.

I've modified my test case slightly to verify that it is `int'.

[vitek@sidewinder] 71 % cat t.cpp
#include <stdio.h>

enum E {
    E_val  = 1,
};

enum U {
    U_val  = 4294967295,
};

inline constexpr E operator~(E e)
{
    return E(~static_cast<int>(e));
}

#define DEFINE_DETECT(T) const char* detect(T) { return #T; }
DEFINE_DETECT(bool)

DEFINE_DETECT(char)
DEFINE_DETECT(signed char)
DEFINE_DETECT(unsigned char)

DEFINE_DETECT(wchar_t)

DEFINE_DETECT(short)
DEFINE_DETECT(int)
DEFINE_DETECT(long)
DEFINE_DETECT(long long)

DEFINE_DETECT(unsigned int)
DEFINE_DETECT(unsigned short)
DEFINE_DETECT(unsigned long)
DEFINE_DETECT(unsigned long long)


int main()
{
    printf ("%s\n", detect(E_val));
    printf ("%s\n", detect(U_val));

    int eval = ~E_val;
    int uval = ~U_val;

    (void) eval;
    (void) uval;
}
[vitek@sidewinder] 72 % g++ --pedantic -std=c++11 t.cpp
t.cpp: In function 'int main()':
t.cpp:41:17: warning: overflow in implicit constant conversion [-Woverflow]
     int eval = ~E_val;
                 ^
[vitek@sidewinder] 73 % ./a.out
int
unsigned int


[vitek@sidewinder] 74 %


If the underlying type of `E' is `int', then there should be no overflow when
converting back from `E' to `int', right? What am I missing?

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]