GCC Bugzilla has been upgraded from version 4.4.9 to 5.0rc3. If you see any problem, please report it to bug 64968.
Bug 47570 - [C++0x] "one() >= 0" isn't constexpr for unsigned int, yet == and > is.
Summary: [C++0x] "one() >= 0" isn't constexpr for unsigned int, yet == and > is.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: 4.6.1
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-02-01 11:25 UTC by Simon Hill
Modified: 2011-03-29 14:54 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-unknown-linux-gnu
Build:
Known to work: 4.7.0
Known to fail: 4.6.0
Last reconfirmed: 2011-03-16 20:44:26


Attachments
cxp_geq_uint.cpp (192 bytes, text/x-c++src)
2011-02-01 11:25 UTC, Simon Hill
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Hill 2011-02-01 11:25:00 UTC
Created attachment 23194 [details]
cxp_geq_uint.cpp

I reduced the code down as far as I could. It's fairly self-explanatory.

cxp_geq_uint.cpp
============================
unsigned int constexpr one()
  { return 1; }

int constexpr one_B()
  { return 1; }

int main()
  {
    // FAIL TO COMPILE:
    //static bool constexpr SC_huh = ((unsigned int)one()) >= ((unsigned int)0);
    //static bool constexpr SC_huh = one() >= ((unsigned int)0);
    static bool constexpr SC_huh = one() >= 0;
    
    // COMPILE OK:
    //static bool constexpr SC_huh = ((one() == 0) || (one() > 0));
    //static bool constexpr SC_huh = one() == 0;
    //static bool constexpr SC_huh = one() > 0;
    //static bool constexpr SC_huh = one_B() >= 0;
    //static bool constexpr SC_huh = one() >= 1;
    
    return SC_huh;
  }
==============================
g++ cxp_geq_uint.cpp --std=c++0x
==============================
cxp_geq_uint.cpp: In function ‘int main()’:
cxp_geq_uint.cpp:12:43: error: ‘(1u, true)’ is not a constant expression
==============================

I can see no reason why >= shouldn't be constexpr in this case.

IIRC standard operators and literal casts always have defined behaviour for standard unsigned integers.

As shown in the code, the problem goes away if:
- The value to the right of >= is greater than 0.
- The type is changed to (regular) int.
- The operation is changed to == or > (or combining these manually to form >=)


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


I searched the buglist for constexpr & unsigned, didn't see any dupes myself.


I guess this one probably won't be too hard to debug.
I might even have a go if I get some spare time.

HTH.
Simon.
Comment 1 Jason Merrill 2011-03-17 02:36:07 UTC
Author: jason
Date: Thu Mar 17 02:36:04 2011
New Revision: 171083

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171083
Log:
	PR c++/47570
	* semantics.c (cxx_eval_constant_expression) [COMPOUND_EXPR]: Don't
	use the generic binary expression handling.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-47570.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 2 Jason Merrill 2011-03-17 02:41:15 UTC
Fixed for 4.7.  I'll backport the patch to 4.6.1 after 4.6.0 is released.
Comment 3 Jason Merrill 2011-03-29 14:24:15 UTC
Author: jason
Date: Tue Mar 29 14:24:09 2011
New Revision: 171663

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171663
Log:
	PR c++/47570
	* semantics.c (cxx_eval_constant_expression) [COMPOUND_EXPR]: Don't
	use the generic binary expression handling.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-47570.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/semantics.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 4 Jason Merrill 2011-03-29 14:37:44 UTC
Fixed for 4.6.1.