#1677 C++11 raw literals are not constant expressions

OTHER
open
nobody
Bug
none
Known_bugs
False
2014-08-19
2012-11-03
thomas
No

C++11 raw literals are not constant expressions (although they obviously are). Possibly related to artefact 3471328 ("__func__ breaks constexpr (C++11)").

Test code: Code converts a binary literal to an integer via raw literal. The static_assert is meant to guarantee that literals which are not binary numbers will cause a compilation failure rather than producing a bogus value. Since the compiler constructs str from a literal, it is demonstrably constant. However, the compiler will not accept it inside static_assert.

#include <stdio.h>

constexpr int valid_bin_number(const char* c) { return *c ? ((*c == '1' || *c == '0') ? valid_bin_number(c+1) : false ) : true; }

unsigned int operator"" _bin(const char* str)
{
static_assert(valid_bin_number(str), "not a binary number");

unsigned int ret = 0;

for\(unsigned int i = 0; str\[i\] \!= '\0'; ++i\)
\{
    char digit = str\[i\];
    ret = ret \* 2 + \(digit - '0'\);
\}
return ret;

}

int main()
{
unsigned int a = 10000_bin;
(void) a;
return 0;
}

Output:
----------------
In function 'unsigned int operator"" _bin(const char*)':
error: non-constant condition for static assertion
error: 'str' is not a constant expression

Discussion

  • Earnie Boyd
    Earnie Boyd
    2012-11-03

    • status: open --> pending-remind
     
  • Earnie Boyd
    Earnie Boyd
    2012-11-03

    Thomas, please submit your bug to the gcc bugs tracker and cross reference the tracker link here. See http://gcc.gnu.org/bugs/ for information on that. We currently do not have a gcc maintainer, do you know someone who could fill that role?

     
  • thomas
    thomas
    2012-11-21

    • status: pending-remind --> open-remind
     
  • Earnie Boyd
    Earnie Boyd
    2013-02-04

    • status: open-remind --> open
    • resolution: --> none
    • category: --> Known_bugs
    • milestone: --> MSYS
     
  • Earnie Boyd
    Earnie Boyd
    2013-02-11

    • labels: gcc-4.7.0 --> gcc-4.7.0, gcc, g++
    • milestone: MSYS --> OTHER
    • type: --> Bug
    • patch_attached: --> False