[Bug c++/65398] [5 Regression] [C++11] GCC rejects constexpr variable definitions with valid initialization

kariya_mitsuru at hotmail dot com gcc-bugzilla@gcc.gnu.org
Tue Mar 31 16:19:00 GMT 2015


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

--- Comment #13 from Mitsuru Kariya <kariya_mitsuru at hotmail dot com> ---
Thank you for your quick response.
But unfortunately, I found problems like below.

========================== sample code 1 ==========================
constexpr char f(const char* s)
{
        return *(s + 1);
}

constexpr char s[] = "ss";

constexpr auto c = f(&s[0]);

int main() {}
========================== sample code 1 ==========================
========================= error message 1 =========================
sample1.cpp:8:21:   in constexpr expansion of 'f((& s[0]))'
sample1.cpp:8:27: error: '*(s + 1u)' is not a constant expression
 constexpr auto c = f(&s[0]);
                           ^
========================= error message 1 =========================

========================== sample code 2 ==========================
constexpr char s1[] = "s1";
constexpr char s2[] = "s2";

constexpr auto eq = (s1 + 0) == (s2 + 1);

int main() {}
========================== sample code 2 ==========================
========================= error message 2 =========================
sample2.cpp:4:30: error: '(((const char*)(& s1)) == (((const char*)(& s2)) +
1u))' is not a constant expression
 constexpr auto eq = (s1 + 0) == (s2 + 1);
                              ^
========================= error message 2 =========================

========================== sample code 3 ==========================
constexpr bool g(char const* found, char const* last) {
        return found == last;
}

constexpr bool f(const char* s)
{
        return g(s, s + 1);
}

constexpr char s[] = "ss";

constexpr auto b = f(&s[0]);

int main() {}
========================== sample code 3 ==========================
========================= error message 3 =========================
sample3.cpp:2:15: error: '((& s[0]) == ((& s[0]) + 1u))' is not a constant
expression
  return found == last;
               ^
========================= error message 3 =========================

The sample code 1 is compiled successfully on both the 4.9.2 and the previous
revision but it causes an error on the head revision.
The sample code 2 causes an error on the 4.9.2, the previous revision and the
head revision.
The sample code 3 is compiled successfully on the 4.9.2, but it causes an error
on both the previous revision and the head revision.

(I use a constexpr library and its tests are passed on 4.9.2 but are failed on
5.0.0.)



More information about the Gcc-bugs mailing list