Попытка сделать пользовательский формат как std :: string, так и std :: wstring [закрыто]C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Попытка сделать пользовательский формат как std :: string, так и std :: wstring [закрыто]

Сообщение Anonymous »

Я использую Visual C ++ 2019 с C ++ 20.
У меня есть пользовательский тип Rational . Мне удалось создать пользовательский форматер для создания значений std :: string . Теперь я хотел бы создать форматер для значений std :: wstring . В зависимости от настройки, я либо получаю «попытку ссылаться на удаленную функцию», либо «невозможно разрешить перегрузку». 1-5/8 .
static const std::vector supers{ L'\u2070', L'\u00B9', L'\u00B2', L'\u00B3', L'\u2074', L'\u2075',
L'\u2076', L'\u2077', L'\u2078', L'\u2079', L'\u207A', L'\u207B' };
#include
template struct std::formatter
{
bool isHex{ false };
bool isFraction{ false };
bool isFloat{ false };
constexpr auto parse (std::format_parse_context &ctx)
{
auto pos = ctx.begin ();
while ((pos != ctx.end ()) && (*pos != '}'))
{
if (('f' == *pos) || ('F' == *pos))
isFloat = true;
if (('h' == *pos) || ('H' == *pos))
isHex = true;
if (('r' == *pos) || ('R' == *pos))
{
isFraction = true;
}
++pos;
}
return pos; // expect `}` at this position, otherwise,
// it's error! exception!
;
}
auto format (const Rational &obj, std::format_context &ctx) const
{
if (1 == obj.den)
return std::format_to (ctx.out (), "{0}", obj.num);
if (isFloat)
return std::format_to (ctx.out (), "{0:f}", (float) obj.num / obj.den);
if (isFraction)
{
if (std::abs (obj.num) > obj.den)
{
lldiv_t q = std::div ((long long) obj.num, (long long) obj.den);
return std::format_to (ctx.out (), "{0}-{1}/{2}", q.quot, q.rem, obj.den);
}
}
return std::format_to (ctx.out (), "{0}/{1}", obj.num, obj.den);
}
};
#if 0

template
struct std::formatter: std::formatter{
bool isHex{ false };
bool isFraction{ false };
bool isFloat{ false };
/* constexpr*/ auto parse (std::wformat_parse_context &ctx)
{
auto pos = ctx.begin ();
while ((pos != ctx.end ()) && (*pos != L'}'))
{
if ((L'f' == *pos) || (L'F' == *pos))
isFloat = true;
if ((L'h' == *pos) || (L'H' == *pos))
isHex = true;
if ((L'r' == *pos) || (L'R' == *pos))
{
isFraction = true;
}
++pos;
}
return pos; // expect `}` at this position, otherwise,
// it's error! exception!
;
}
auto format (const Rational &obj, std::wformat_context &ctx) const
{
if (1 == obj.den)
return std::format_to (ctx.out (), L"{0}", obj.num, obj.den);
if (isFloat)
return std::format_to (ctx.out (), L"{0}/{1}", obj.num, obj.den);
if (isFraction)
{
auto temp = obj.num;
std::wstring temps{};
if (obj.num < 0)
{
temp = -obj.num;
temps += supers[0xb];
}
if (std::abs (obj.num) > obj.den)
{
lldiv_t q = std::div ((long long) obj.num, (long long) obj.den);
temps = std::format (L"{0}", q.quot);
temp = ((T) (q.rem));
}
std::wstring temp_ = std::format (L"{}", temp);
for (auto t : temp_)
{
temps += supers[t - L'0'];
}
temps += L"/";
temp_ = std::format (L"{}", obj.den);
for (auto t : temp_)
{
temps += (L'\u2080' + t - L'0');
}
return std::format_to (ctx.out (), "L{0}", temps);
}
return std::format_to (ctx.out (), L"{0}/{1}", obj.num, obj.den);
}
};

< /code>
Ошибки с одним набором кода: < /p>
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2807,1): error C2280: 'std::formatter::formatter(void)': attempting to reference a deleted function
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2889): message : see declaration of 'std::formatter::formatter'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2889,5): message : 'std::formatter::formatter(void)': function was explicitly deleted
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2821): message : see reference to function template instantiation 'std::_String_view_iterator std::_Compile_time_parse_format_specs(_ParseContext &)' being compiled
1> with
1> [
1> _Traits=std::char_traits,
1> _ParseContext=std::basic_format_parse_context
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2821): message : while compiling class template member function 'std::_Format_checker::_Format_checker(std::basic_string_view) noexcept'
1> with
1> [
1> _CharT=wchar_t
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2988): message : see reference to function template instantiation 'std::_Format_checker::_Format_checker(std::basic_string_view) noexcept' being compiled
1> with
1> [
1> _CharT=wchar_t
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2986): message : see reference to class template instantiation 'std::_Format_checker' being compiled
1> with
1> [
1> _CharT=wchar_t
1> ]
1>C:\Users\michr\source\repos\RationalDemo\RationalDemo.cpp(34): message : see reference to function template instantiation 'std::_Basic_format_string::_Basic_format_string(const _Ty (&))' being compiled
1> with
1> [
1> _Ty=wchar_t [3]
1> ]
< /code>
с одинаковыми определениями, но нет ссылки на Wstring: < /p>
1>------ Build started: Project: RationalDemo, Configuration: Debug x64 ------
1>RationalDemo.cpp
1>RationalDemo.vcxproj -> C:\Users\michr\source\repos\RationalDemo\x64\Debug\RationalDemo.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
< /code>
.... и с другим набором кода < /p>
Build started...
1>------ Build started: Project: RationalDemo, Configuration: Debug x64 ------
1>RationalDemo.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1516,1): error C2665: 'std::_Format_arg_traits::_Phony_basic_format_arg_constructor': none of the 5 overloads could convert all the argument types
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1507,17): message : could be 'const void *std::_Format_arg_traits::_Phony_basic_format_arg_constructor(std::nullptr_t)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1497,17): message : or 'const char *std::_Format_arg_traits::_Phony_basic_format_arg_constructor(const char *)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1495,17): message : or 'long double std::_Format_arg_traits::_Phony_basic_format_arg_constructor(long double)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1494,17): message : or 'double std::_Format_arg_traits::_Phony_basic_format_arg_constructor(double)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1493,17): message : or 'float std::_Format_arg_traits::_Phony_basic_format_arg_constructor(float)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1516,1): message : while trying to match the argument list '(std::wstring)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1519): message : see reference to alias template instantiation 'std::_Format_arg_traits::_Storage_type' being compiled
1> with
1> [
1> _Ty=std::wstring &
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1554): message : see reference to variable template 'const size_t std::_Format_arg_traits::_Storage_size' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(3003): message : see reference to class template instantiation 'std::_Format_arg_store' being compiled
1> with
1> [
1> _Context=std::format_context
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(3069): message : see reference to function template instantiation 'auto std::make_format_args(std::wstring &)' being compiled
1>C:\Users\michr\source\repos\RationalDemo\Rational.h(253): message : see reference to function template instantiation '_OutputIt std::format_to(_OutputIt,const std::_Basic_format_string,std::wstring &)' being compiled
1> with
1> [
1> _OutputIt=std::back_insert_iterator
1> ]
1>C:\Users\michr\source\repos\RationalDemo\Rational.h(222): message : while compiling class template member function '_OutputIt std::formatter::format(const Rational &,std::wformat_context &) const'
1> with
1> [
1> _OutputIt=std::back_insert_iterator,
1> _CharT=wchar_t
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(173): message : see reference to class template instantiation 'std::formatter' being compiled
1> with
1> [
1> _CharT=wchar_t
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1469): message : see reference to variable template 'bool _Has_formatter' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1519): message : see reference to alias template instantiation 'std::_Format_arg_traits::_Storage_type' being compiled
1> with
1> [
1> _Context=std::wformat_context,
1> _Ty=sRat &
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(1554): message : see reference to variable template 'const size_t std::_Format_arg_traits::_Storage_size' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(3008): message : see reference to class template instantiation 'std::_Format_arg_store' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(3136): message : see reference to function template instantiation 'std::_Format_arg_store std::make_wformat_args(sRat &)' being compiled
1>C:\Users\michr\source\repos\RationalDemo\RationalDemo.cpp(34): message : see reference to function template instantiation 'std::wstring std::format(const std::_Basic_format_string,sRat &)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2806,1): error C2993: 'unknown-type': is not a valid type for non-type template parameter '_Test'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2807,49): error C2641: cannot deduce template arguments for 'std::formatter'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2807,49): error C2783: 'std::formatter std::formatter(void)': could not deduce template argument for '_Ty'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2889): message : see declaration of 'std::formatter'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2807,1): error C2780: 'std::formatter std::formatter(std::formatter)': expects 1 arguments - 0 provided
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2888): message : see declaration of 'std::formatter'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2808,23): error C2039: 'parse': is not a member of 'std::formatter'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\format(2888): message : see declaration of 'std::formatter'
1>Done building project "RationalDemo.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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

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

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

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

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

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Попытка сделать пользовательский формат как Std :: String, так и Std :: Wstring
    Anonymous » » в форуме C++
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • `std::map<std::string, enum{1, 2, 3, 4}>` (или std::map<std::string, tuple<bool, bool>>`) кэширует` против `std: :set<st
    Anonymous » » в форуме C++
    0 Ответы
    326 Просмотры
    Последнее сообщение Anonymous
  • Назначьте ввод Map> выходу Map>
    Anonymous » » в форуме JAVA
    0 Ответы
    125 Просмотры
    Последнее сообщение Anonymous
  • Конвертировать Map > для списка > используя API потока
    Anonymous » » в форуме JAVA
    0 Ответы
    101 Просмотры
    Последнее сообщение Anonymous
  • Непонятное поведение std::next_permutation с std::wstring
    Anonymous » » в форуме C++
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous

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