в 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 | = __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 | = __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 | { __cf.format(__t, __fc) } -> same_as;
| ^
1 error generated.
Подробнее здесь: https://stackoverflow.com/questions/794 ... ormattable
Мобильная версия