Статический член данных типа класса шаблона: constexpr против const constinitC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Статический член данных типа класса шаблона: constexpr против const constinit

Сообщение Anonymous »

У меня есть класс:

Код: Выделить всё

#include 

template
requires std::is_arithmetic_v && (N >= 1)
class Vector
{
static constexpr std::size_t Dimension = N;
std::array Elements;

public:
constexpr Vector() noexcept : Elements{} {}
constexpr ~Vector() = default;
static constexpr Vector ZeroVector{};
};

int main()
{
Vector boo = Vector::ZeroVector;
}
Приведенное выше не удается скомпилировать в обозревателе компилятора с MSVC и Clang (магистраль) с флагами компилятора C++23, но оно компилируется в GCC (магистраль) с флагами компилятора C++23.
Clang выдает следующую ошибку:

Код: Выделить всё

:13:29: error: constexpr variable cannot have non-literal type 'const Vector'
13 |     static constexpr Vector ZeroVector{};
|                             ^
:18:28: note: in instantiation of template class 'Vector' requested here
18 |     Vector boo = Vector::ZeroVector;
|                            ^
:13:29: note: incomplete type 'const Vector' is not a literal type
13 |     static constexpr Vector ZeroVector{};
|                             ^
:5:7: note: definition of 'Vector' is not complete until the closing '}'
5 | class Vector
|       ^
1 error generated.
Compiler returned: 1
MSVC выдает следующую ошибку:

Код: Выделить всё

(13): error C2027: use of undefined type 'Vector'
(5): note: see declaration of 'Vector'
(13): note: the template instantiation context (the oldest one first) is
(18): note: see reference to class template instantiation 'Vector' being compiled
Compiler returned: 2
Когда я меняю constexpr на const constinit для ZeroVector, это компилируется на всех трех основных компиляторах, когда определение перемещается за пределы класса, вот так :

Код: Выделить всё

template requires std::is_arithmetic_v && (N >= 1)
const constinit Vector Vector::ZeroVector{};
Так почему constexpr компилируется только в GCC, а const constinit компилируется во всех трех основных компиляторах?

Подробнее здесь: https://stackoverflow.com/questions/782 ... -constinit
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»