Код: Выделить всё
template
class small_vector {
....
class view {
...
};
};
Код: Выделить всё
template
using small_vector_view = typename small_vector::view;
template struct std::formatter {
enum class Style { Default, Compact, Pretty };
Style style = Style::Default;
constexpr auto parse(format_parse_context &ctx)
{
auto it = ctx.begin();
if (it == ctx.end() || *it == '}')
return it;
switch (*it) {
case 'c':
style = Style::Compact;
break;
case 'p':
style = Style::Pretty;
break;
default:
throw format_error("invalid format specifier for small_vector");
}
return ++it;
}
auto format(const typename small_vector::view &v, format_context &ctx) const
{
if (v.empty()) {
if (style == Style::Pretty) {
return format_to(ctx.out(), "[\n]");
}
return format_to(ctx.out(), "[]");
}
std::string result;
if (style == Style::Pretty) {
result = "[\n ";
}
else {
result = "[";
}
for (size_t i = 0; i < v.size(); ++i) {
if (i > 0) {
if (style == Style::Pretty) {
result += ",\n ";
}
else if (style == Style::Compact) {
result += ",";
}
else {
result += ", ";
}
}
if constexpr (std::is_same_v) {
result += std::format("\"{}\"", v[i]);
}
else {
result += std::format("{}", v[i]);
}
}
if (style == Style::Pretty) {
result += "\n]";
}
else {
result += "]";
}
return format_to(ctx.out(), "{}", result);
}
};
Код: Выделить всё
D:\repos\ustring\small_vector.h(602): error C2764: 'T': template parameter not used or deducible in partial specialization 'std::formatter'
D:\repos\ustring\small_vector.h(602): error C2764: 'N': template parameter not used or deducible in partial specialization
'std::formatter'
D:\repos\ustring\small_vector_tests.cpp(557): error C7595: 'std::basic_format_string::basic_format_string': call to immediate function is not a constant expression
(полный код: https://github.com/nekomiya-kasane/temp ... l_vector.h, последний 2 функции)
Подробнее здесь: https://stackoverflow.com/questions/793 ... nt-compile
Мобильная версия