1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify
static_assert(true, "");
static_assert(true, 0); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}}
struct Empty{};
static_assert(true, Empty{}); // expected-error {{the message object in this static assertion is missing 'data()' and 'size()' member functions}}
struct NoData {
unsigned long size() const;
};
struct NoSize {
const char* data() const;
};
static_assert(true, NoData{}); // expected-error {{the message object in this static assertion is missing a 'data()' member function}}
static_assert(true, NoSize{}); // expected-error {{the message object in this static assertion is missing a 'size()' member function}}
struct InvalidSize {
const char* size() const;
const char* data() const;
};
static_assert(true, InvalidSize{}); // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
// expected-error {{value of type 'const char *' is not implicitly convertible to 'unsigned long'}}
struct InvalidData {
unsigned long size() const;
unsigned long data() const;
};
static_assert(true, InvalidData{}); // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \
// expected-error {{value of type 'unsigned long' is not implicitly convertible to 'const char *'}}
struct NonConstexprSize {
unsigned long size() const; // expected-note 2{{declared here}}
constexpr const char* data() const;
};
static_assert(true, NonConstexprSize{}); // expected-error {{the message in this static assertion is not a constant expression}} \
// expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
static_assert(false, NonConstexprSize{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
// expected-error {{static assertion failed}} \
// expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
struct NonConstexprData {
constexpr unsigned long size() const {
return 32;
}
const char* data() const; // expected-note 2{{declared here}}
};
static_assert(true, NonConstexprData{}); // expected-error {{the message in this static assertion is not a constant expression}} \
// expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
static_assert(false, NonConstexprData{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
// expected-error {{static assertion failed}} \
// expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
struct string_view {
int S;
const char* D;
constexpr string_view(const char* Str) : S(__builtin_strlen(Str)), D(Str) {}
constexpr string_view(int Size, const char* Str) : S(Size), D(Str) {}
constexpr int size() const {
return S;
}
constexpr const char* data() const {
return D;
}
};
constexpr const char g_[] = "long string";
template <typename T, int S>
struct array {
constexpr unsigned long size() const {
return S;
}
constexpr const char* data() const {
return d_;
}
const char d_[S];
};
static_assert(false, string_view("test")); // expected-error {{static assertion failed: test}}
static_assert(false, string_view("😀")); // expected-error {{static assertion failed: 😀}}
static_assert(false, string_view(0, nullptr)); // expected-error {{static assertion failed:}}
static_assert(false, string_view(1, "ABC")); // expected-error {{static assertion failed: A}}
static_assert(false, string_view(42, "ABC")); // expected-error {{static assertion failed: ABC}} \
// expected-error {{the message in a static assertion must be produced by a constant expression}} \
// expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
static_assert(false, array<char, 2>{'a', 'b'}); // expected-error {{static assertion failed: ab}}
struct ConvertibleToInt {
constexpr operator int() {
return 4;
}
};
struct ConvertibleToCharPtr {
constexpr operator const char*() {
return "test";
}
};
struct MessageFromConvertible {
constexpr ConvertibleToInt size() const {
return {};
}
constexpr ConvertibleToCharPtr data() const {
return {};
}
};
static_assert(true, MessageFromConvertible{});
static_assert(false, MessageFromConvertible{}); // expected-error{{static assertion failed: test}}
struct Leaks {
constexpr unsigned long size() const {
return 2;
}
constexpr const char* data() const {
return new char[2]{'u', 'b'}; // expected-note {{allocation performed here was not deallocated}}
}
};
static_assert(false, Leaks{}); //expected-error {{the message in a static assertion must be produced by a constant expression}} \
// expected-error {{static assertion failed: ub}}
struct RAII {
const char* d = new char[2]{'o', 'k'};
constexpr unsigned long size() const {
return 2;
}
constexpr const char* data() const {
return d;
}
constexpr ~RAII() {
delete[] d;
}
};
static_assert(false, RAII{}); // expected-error {{static assertion failed: ok}}
namespace MoreTemporary {
struct Data{
constexpr operator const char*() const {
return d;
}
char d[6] = { "Hello" };
};
struct Size {
constexpr operator int() const {
return 5;
}
};
struct Message {
constexpr auto size() const {
return Size{};
}
constexpr auto data() const {
return Data{};
}
};
static_assert(false, Message{}); // expected-error {{static assertion failed: Hello}}
}
struct MessageInvalidSize {
constexpr auto size(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
constexpr auto data() const;
};
struct MessageInvalidData {
constexpr auto size() const;
constexpr auto data(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
};
static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \
// expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}
static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \
// expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
struct NonConstMembers {
constexpr int size() {
return 1;
}
constexpr const char* data() {
return "A";
}
};
static_assert(false, NonConstMembers{}); // expected-error {{static assertion failed: A}}
struct DefaultArgs {
constexpr int size(int i = 0) {
return 2;
}
constexpr const char* data(int i =0, int j = 42) {
return "OK";
}
};
static_assert(false, DefaultArgs{}); // expected-error {{static assertion failed: OK}}
struct Variadic {
constexpr int size(auto...) {
return 2;
}
constexpr const char* data(auto...) {
return "OK";
}
};
static_assert(false, Variadic{}); // expected-error {{static assertion failed: OK}}
template <typename T>
struct DeleteAndRequires {
constexpr int size() = delete; // expected-note {{candidate function has been explicitly deleted}}
constexpr const char* data() requires false; // expected-note {{candidate function not viable: constraints not satisfied}} \
// expected-note {{because 'false' evaluated to false}}
};
static_assert(false, DeleteAndRequires<void>{});
// expected-error@-1 {{static assertion failed}} \
// expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\
// expected-error@-1 {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
class Private {
constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}
return 2;
}
constexpr const char* data(int i =0, int j = 42) { // expected-note {{implicitly declared private here}}
return "OK";
}
};
static_assert(false, Private{}); // expected-error {{'data' is a private member of 'Private'}}\
// expected-error {{'size' is a private member of 'Private'}}\
// expected-error {{static assertion failed: OK}}
struct MessageOverload {
constexpr int size() {
return 1;
}
constexpr int size() const;
constexpr const char* data() {
return "A";
}
constexpr const char* data() const;
};
static_assert(false, MessageOverload{}); // expected-error {{static assertion failed: A}}
struct InvalidPtr {
consteval auto size() {
return 42;
}
consteval const char *data() {
const char *ptr; // Garbage
return ptr; // expected-note {{read of uninitialized object is not allowed in a constant expression}}
}
};
static_assert(false, InvalidPtr{}); // expected-error{{the message in a static assertion must be produced by a constant expression}} \
//expected-error {{static assertion failed}} \
// expected-note {{in call to 'InvalidPtr{}.data()'}}
namespace DependentMessage {
template <typename Ty>
struct Good {
static_assert(false, Ty{}); // expected-error {{static assertion failed: hello}}
};
template <typename Ty>
struct Bad {
static_assert(false, Ty{}); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}} \
// expected-error {{static assertion failed}}
};
struct Frobble {
constexpr int size() const { return 5; }
constexpr const char *data() const { return "hello"; }
};
Good<Frobble> a; // expected-note {{in instantiation}}
Bad<int> b; // expected-note {{in instantiation}}
}
|