recortar para std :: string y std :: wstring [duplicate]
Sep 09 2020
Tengo una función de recorte para std :: string,
static string trim(const string &s)
{
int ibegin = s.find_first_not_of(" \t\r\n");
if (ibegin == string::npos)
{
return "";
}
int iend = s.find_last_not_of(" \t\r\n");
return s.substr(ibegin, iend - ibegin);
}
Quiero adaptarlo tanto para string como para wstring,
template<typename TCHAR>
std::basic_string<TCHAR> trim(const std::basic_string<TCHAR>& s)
{
#if (sizeof(TCHAR)==1)
int ibegin = s.find_first_not_of(" \t\r\n");
#else
int ibegin = s.find_first_not_of(L" \t\r\n");
#endif
...
}
Pero no se compila. sizeof en el comando del preprocesador no se compila con el error C1017
Respuestas
2 vll Sep 09 2020 at 16:22
TCHARes el argumento de la plantilla, no un token de preprocesador. Úselo en su if constexprlugar.
template<typename TCHAR>
std::basic_string<TCHAR> trim(const std::basic_string<TCHAR>& s)
{
int ibegin;
if constexpr(sizeof(TCHAR) == 1) {
ibegin = s.find_first_not_of(" \t\r\n");
}
else {
ibegin = s.find_first_not_of(L" \t\r\n");
}
...
}