Что мне нужно сделать, чтобы сделать класс Enum Std :: Formattable?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Что мне нужно сделать, чтобы сделать класс Enum Std :: Formattable?

Сообщение Anonymous »

Я собираюсь переключиться с C ++ 20 на C ++ 23, который имеет std :: formattable .
в c ++ 20, я использовал Formattable от https://stackoverflow.com/a/72430675, но оказывается std :: formattable работает по -разному. По сути, классы, которые форматируемые не обязательно являются std :: formattable , и мне интересно, почему. Вот один пример: < /p>
#include
#include
#include

enum class RandomEnum { A, B };

// Formatter that formats everything as "ABC"
template
struct std::formatter : std::formatter {
auto format(const RandomEnum t, std::format_context& ctx) const {
return std::formatter::format("ABC", ctx);
}
};

// Pre-C++23 version of std::formattable
template
concept formattable = requires(T& v, std::format_context ctx) {
std::formatter().format(v, ctx);
};

int main() {
// Returns 2: class is formattable but not std::formattable:
return (formattable
#include
#include
#include

enum class RandomEnum { A, B };

template
struct std::formatter : std::formatter {
auto format(const RandomEnum t, std::format_context& ctx) const {
return std::formatter::format("ABC", ctx);
}
};

int fun(std::formattable auto x) { return 0; }

int main() { return fun(RandomEnum::A); }
< /code>
msvc: < /p>
(16): error C2672: 'fun': no matching overloaded function found
(14): note: could be 'int fun(_T0)'
(16): note: the associated constraints are not satisfied
(14): note: the concept 'std::formattable' evaluated to false
Z:/compilers/msvc/14.41.33923-14.41.33923.0/include\format(2255): note: the concept 'std::_Formattable_with' evaluated to false
Z:/compilers/msvc/14.41.33923-14.41.33923.0/include\format(658): note: 'std::back_insert_iterator std::formatter::format(const RandomEnum,std::format_context &) const': cannot convert argument 2 from 'std::basic_format_context' to 'std::format_context &'
Z:/compilers/msvc/14.41.33923-14.41.33923.0/include\format(658): note: while trying to match the argument list '(RandomEnum, std::basic_format_context)'
< /code>
clang: < /p>
:16:21: error: no matching function for call to 'fun'
16 | int main() { return fun(RandomEnum::A); }
| ^~~
:14:5: note: candidate template ignored: constraints not satisfied [with x:auto = RandomEnum]
14 | int fun(std::formattable auto x) { return 0; }
| ^
:14:9: note: because 'std::formattable' evaluated to false
14 | int fun(std::formattable auto x) { return 0; }
| ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/format:2548:9: note: because '__format::__formattable_impl' evaluated to false
2548 | = __format::__formattable_impl;
| ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/format:2539:43: note: because '__formattable_with' evaluated to false
2539 | = __parsable_with && __formattable_with;
| ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/format:2529:26: note: because '__cf.format(__t, __fc)' would be invalid: non-const lvalue reference to type 'basic_format_context' cannot bind to a value of unrelated type 'basic_format_context'
2529 | { __cf.format(__t, __fc) } -> same_as;
| ^
1 error generated.


Подробнее здесь: https://stackoverflow.com/questions/794 ... ormattable
Ответить

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

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

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

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

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