Код: Выделить всё
#include
static constexpr std::string FIRST = "qqqq.";
//static constexpr std::string SECOND = "789.aaaa-bbbb-cccc";
constexpr bool do_something() {
std::string name = FIRST;
return true;
}
int main() {
static_assert(do_something());
}
< /code>
Но они не компилируют следующее (осмотрите его на компиляторе Explorer здесь) < /p>
#include
static constexpr std::string FIRST = "qqqq.";
static constexpr std::string SECOND = "789.aaaa-bbbb-cccc";
constexpr bool do_something() {
std::string name = FIRST;
return true;
}
int main() {
static_assert(do_something());
}
:4:30: error: constexpr variable 'SECOND' must be initialized by a constant expression
4 | static constexpr std::string SECOND = "789.aaaa-bbbb-cccc";
| ^ ~~~~~~~~~~~~~~~~~~~~
:4:30: note: pointer to subobject of heap-allocated object is not a constant expression
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/bits/alloc_traits.h:478:20: note: heap allocation performed here
478 | { return __a.allocate(__n); }
| ^
1 error generated.
Compiler returned: 1
< /code>
выглядит как синтаксически и семантически правильные примеры. Почему один составлен, но не другой? Что такое «субобект объекта с расплавленным кучей»? (только ссылка, которую я мог найти здесь)
Подробнее здесь: https://stackoverflow.com/questions/797 ... th-just-on
Мобильная версия