9
struct A {
    int a = 0;
    constexpr A() { a = 1; }
};

constexpr bool f() {
    constexpr A a;
    static_assert(a.a == 1, ""); // L1: OK
    return a.a == 1;
}
static_assert(f(), ""); // L2: Error, can not modify A::a in constexpr

If I delete L2, this code compiles. If I add L2, the compiler complained "modification of object of const-qualified type 'const int' is not allowed in a constant expression". I am not a language lawyer, so I am not sure whether this is true. However, if it is, why compiler didn't complain anything about L1, since it also called A() as constexpr? Is this a bug of clang? Or did I miss anything?

Reference: http://en.cppreference.com/w/cpp/language/constexpr

BTW, if I change "constexpr A a;" to "A a;" (remove constexpr keyword), L1 failed to compile which is expect. However, the compiler didn't complain about L2 anymore.

Online Compiler URL about this: http://goo.gl/AoTzYx

1 Answer 1

4

I believe this is just a case of compilers not having caught up to the changes proposed for C++14. Your constexpr constructor satisfies all the conditions listed in §7.1.5/4 of N3936. Both gcc and clang fail to compile your code, but for different reasons.

clang complains:

note: modification of object of const-qualified type 'const int' is not allowed in a constant expression

which doesn't make much sense, but reminds me of the C++11 restriction that constexpr member functions are implicitly const (this is a constructor, and that doesn't apply, but the error message is reminiscent of that). This restriction was also lifted for C++14.

gcc's error message is:

error: constexpr constructor does not have empty body

Seems pretty clear that gcc still implements the C++11 rules for constexpr constructors.

Moreover, N3597 lists this example:

struct override_raii {
  constexpr override_raii(int &a, int v) : a(a), old(a) {
    a = v;
  }
  constexpr ~override_raii() {
    a = old;
  }
  int &a, old;
};

N3597 was superseded by N3652, which contains the wording found in the current draft. Unfortunately, the earlier example disappears, but, again, nothing in the current wording says you cannot assign values to data members within the body of a constexpr constructor.

Update (2017-10-03)

clang fixed this, but there has been no new release yet: https://bugs.llvm.org/show_bug.cgi?id=19741 (Compiler explorer)

8
  • would it help to assign A::a using initializer syntax instead of in the constructor's function body?
    – NHDaly
    May 14, 2014 at 8:01
  • @NHDaly Yes, of course. clang successfully compiles the code in that case. gcc still fails because f() doesn't satisfy the C++11 requirements for constexpr functions. The issue here is the assignment of the data member within the body of the constructor.
    – Praetorian
    May 14, 2014 at 8:04
  • And that's just because gcc hasn't caught up with C++14 yet, yeah?
    – NHDaly
    May 14, 2014 at 8:06
  • @NHDaly It would seem so, at least when it comes to relaxed constexpr
    – Praetorian
    May 14, 2014 at 8:07
  • 1
    I don't see how that example applies... It is not assigning a value to a data member in the body of the constructor because a is a reference, no?
    – ildjarn
    May 22, 2014 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.