Использование члена int в качестве параметра шаблона внутри функции-члена constevalC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Гость
 Использование члена int в качестве параметра шаблона внутри функции-члена consteval

Сообщение Гость »


Я пытаюсь использовать member of a

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

constexpr
object as a template parameter. The idea is that I create a

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

constexpr Symbol
and then have that converted into a

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

SymbolRef
(in the real code that 1 is different for different objects).

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

template
struct SymbolRef { };

struct Symbol {
int const id_;

consteval Symbol() : id_(1) { }

template
consteval operator SymbolRef() const
{
return SymbolRef{};
}
};
However, trying to compile the above code I get the error:

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

error: non-type template argument is not a constant expression
12 |     return SymbolRef{};
|                      ^~~
:12:22: note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
The phrase "is only allowed within the evaluation of a call to a 'constexpr' member function" seems to not apply: this is a

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

consteval
function!?
I understand that this code is ill-formed when

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

operator SymbolRef()
would be a

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

constexpr
member function (in spite of the error message!) because in that case the function must also compile without the

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

constexpr
, in which case

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

this->id_
can't be used as a template parameter.
Why is this code illegal when using it with

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

consteval
?
EDIT:
The following does compile with g++ - but only when using

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

this->id_
.

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

template
struct SymbolRef { };

struct Symbol {
int const id_;

consteval Symbol() : id_(1) { }

template
consteval operator SymbolRef() const
{
return SymbolRefid_>{};
}
};
I am guessing it is a compiler bug therefore? It seems that the correct behavior is that it would compile with or without and clang++ is just wrong.


Источник: https://stackoverflow.com/questions/781 ... r-function
Реклама
Ответить Пред. темаСлед. тема

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

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

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

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

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

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