This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers
- From: "yacwroy at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 28 Jan 2011 04:03:08 +0000
- Subject: [Bug c++/47504] New: some constexpr calculations erroneously overflow when using negative numbers
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47504
Summary: some constexpr calculations erroneously overflow when
using negative numbers
Product: gcc
Version: 4.6.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: yacwroy@gmail.com
Target: x86_64-unknown-linux-gnu
Created attachment 23148
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23148
overflow.cpp
Easiest to describe via code:
overflow.cpp
=================================================
char constexpr sub(char arg)
{ return char(arg - char(1)); }
int main()
{ static char constexpr m = sub(-1); }
=================================================
g++ overflow.cpp --std=c++0x
=================================================
overflow.cpp: In function âint main()â:
overflow.cpp:5:36: error: overflow in constant expression [-fpermissive]
This is simply attempting to subtract 1 from -1, using "char"s.
I cannot see any reason why this should trigger an overflow error.
I have attempted to cast every intermediate state to char to avoid any
unintended casts.
The code is as simple as I could get it while still triggering the seemingly
erroneous overflow.
Compiles OK using int instead of char. (int can still erroneously overflow in
other circumstances, though. Would examples be useful?).
Compiles OK using 1 instead of -1.
Compiles OK using a literal -1 instead of a parameter.
Compiles OK if main()::m isn't constexpr.
Attached source code, but it's probably easier to just copy/paste the above.
SPECS:
gcc: version 4.6.0 2010-12-30 (experimental) (svn = 168358)
- manually patched by
(http://gcc.gnu.org/ml/gcc-patches/2007-04/msg00620.html)
- patch shouldn't have any effect here.
ubuntu: 10.10 (64 bit)
intel: core2 duo
Are any other specs relevant here, such as GMP. (Note: my GMP is the standard
one).
I searched other constexpr bugs here - AFAIK none appear to be about overflow.