| 1 |
// RUN: %clang_cc1 -std=c++11 -verify=expected,expected11,both,both11 %s -fexperimental-new-constant-interpreter |
| 2 |
// RUN: %clang_cc1 -std=c++14 -verify=expected,expected14,both %s -fexperimental-new-constant-interpreter |
| 3 |
// RUN: %clang_cc1 -std=c++11 -verify=ref,ref11,both,both11 %s |
| 4 |
// RUN: %clang_cc1 -std=c++14 -verify=ref,ref14,both %s |
| 5 |
|
| 6 |
namespace Simple { |
| 7 |
struct S { |
| 8 |
mutable int a; // both-note {{declared here}} \ |
| 9 |
// both11-note {{declared here}} |
| 10 |
int a2; |
| 11 |
}; |
| 12 |
|
| 13 |
constexpr S s{12, 24}; |
| 14 |
static_assert(s.a == 12, ""); // both-error {{not an integral constant expression}} \ |
| 15 |
// both-note {{read of mutable member 'a'}} |
| 16 |
static_assert(s.a2 == 24, ""); |
| 17 |
|
| 18 |
|
| 19 |
constexpr S s2{12, s2.a}; // both11-error {{must be initialized by a constant expression}} \ |
| 20 |
// both11-note {{read of mutable member 'a'}} \ |
| 21 |
// both11-note {{declared here}} |
| 22 |
static_assert(s2.a2 == 12, ""); // both11-error {{not an integral constant expression}} \ |
| 23 |
// both11-note {{initializer of 's2' is not a constant expression}} |
| 24 |
} |
| 25 |
#if __cplusplus >= 201402L |
| 26 |
namespace ConstInMutable { |
| 27 |
class B { |
| 28 |
public: |
| 29 |
|
| 30 |
const int f; |
| 31 |
constexpr B() : f(12) {} |
| 32 |
}; |
| 33 |
class A { |
| 34 |
public: |
| 35 |
mutable B b; |
| 36 |
constexpr A() = default; |
| 37 |
}; |
| 38 |
constexpr int constInMutable() { |
| 39 |
A a; |
| 40 |
|
| 41 |
int *m = (int*)&a.b.f; |
| 42 |
*m = 12; // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}} |
| 43 |
return 1; |
| 44 |
} |
| 45 |
static_assert(constInMutable() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 46 |
// both-note {{in call to}} |
| 47 |
} |
| 48 |
|
| 49 |
namespace MutableInConst { |
| 50 |
class C { |
| 51 |
public: |
| 52 |
mutable int c; |
| 53 |
constexpr C() : c(50) {} |
| 54 |
}; |
| 55 |
class D { |
| 56 |
public: |
| 57 |
C c; |
| 58 |
constexpr D() {} |
| 59 |
}; |
| 60 |
constexpr int mutableInConst() { |
| 61 |
const D d{}; |
| 62 |
int *m = (int*)&d.c.c; |
| 63 |
*m = 12; |
| 64 |
return 1; |
| 65 |
} |
| 66 |
static_assert(mutableInConst() == 1, ""); |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
struct D { mutable int y; }; // both-note {{declared here}} |
| 71 |
constexpr D d1 = { 1 }; |
| 72 |
constexpr D d2 = d1; // both-error {{must be initialized by a constant expression}} \ |
| 73 |
// both-note {{read of mutable member 'y}} \ |
| 74 |
// both-note {{in call to}} |
| 75 |
|
| 76 |
|