Создайте std::formatter для класса шаблона для поддержки std::print.C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Создайте std::formatter для класса шаблона для поддержки std::print.

Сообщение Anonymous »

У меня есть структура, определенная внутри класса шаблона, и я хотел бы создать std::formatter для этой структуры, чтобы иметь возможность печатать экземпляры этой структуры с помощью std::print.
У меня класс:

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

template  requires (SizeOfEachStack > 0)
class ThreeStack
{
public:
struct StackProperties
{
std::size_t min_index{0};
std::size_t max_index{0};
std::size_t current_index{0};
};

ThreeStack()
{
for(std::size_t stack_number = 0; stack_number < num_of_stacks; stack_number++)
{
stack_properties.at(stack_number).min_index = stack_number * SizeOfEachStack;
stack_properties.at(stack_number).current_index = stack_properties.at(stack_number).min_index;
stack_properties.at(stack_number).max_index = stack_properties.at(stack_number).min_index + SizeOfEachStack - 1;
}
};

// Print function to print the internal arrays.
void print_internal_arrays()
{
for(std::size_t stack_number = 0; stack_number < num_of_stacks; stack_number++)
{
std::print("Stack {}: {}\n", stack_number, stack_properties.at(stack_number));
}
}

private:
static constexpr size_t num_of_stacks{3};
std::array stack_properties{};
};
А вот созданный мной std::formatter, который не работает:

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

// Create a std::formatter for the ThreeStack::StackProperties struct for any ThreeStack.
template 
struct std::formatter
{
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }

template 
auto format(const ThreeStack::StackProperties& stack_properties, FormatContext& ctx)
{
return format_to(ctx.out(), "min_index: {}, max_index: {}, current_index: {}", stack_properties.min_index, stack_properties.max_index, stack_properties.current_index);
}
};
Я получил следующую ошибку компиляции для этого std::formatter:

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

:43:78: error: type/value mismatch at argument 1 in template parameter list for 'template struct std::formatter'
43 | struct std::formatter
|                                                                              ^
:43:78: note:   expected a type, got 'ThreeStack::StackProperties'
:43:13: error: template class without a name
43 | struct std::formatter
|             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1
https://godbolt.org/z/qeKsMrb4Y

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

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

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

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

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

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

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