Код: Выделить всё
#include
struct Ignore {
constexpr Ignore(auto&& unknown_reference) {}
};
struct X {
constexpr bool f1() { return true; }
static constexpr bool f2() { return true; }
constexpr bool f3(this Ignore) { return true; }
};
consteval bool test(int function) {
std::allocator alloc;
X* p = alloc.allocate(1);
bool result;
switch (function) {
case 1: result = p->f1();
case 2: result = p->f2();
case 3: result = p->f3();
}
alloc.deallocate(p, 1);
return result;
}
#ifndef __clang__
static_assert(test(1));
#endif
static_assert(test(2));
static_assert(test(3));
< /code>
gcc и msvc принимают все три.:26:15: error: static assertion expression is not an integral constant expression
26 | static_assert(test(1));
| ^~~~~~~
:18:25: note: member call on object outside its lifetime is not allowed in a constant expression
18 | case 1: result = p->f1();
| ~~~^~~~
:26:15: note: in call to 'test(1)'
26 | static_assert(test(1));
| ^~~~~~~
Программа имеет неопределенное поведение, если Glvalue используется для вызова нестатической функции объекта
и не F3 как совсем не-сете>
Подробнее здесь: https://stackoverflow.com/questions/796 ... ed-storage