std :: stringとstd :: wstring [重複]の両方のトリム
Sep 09 2020
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);
}
stringと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
...
}
しかし、それはコンパイルされません。プリプロセッサコマンドのsizeofがエラーC1017でコンパイルされない
回答
2 vll Sep 09 2020 at 16:22
TCHARはテンプレート引数であり、プリプロセッサトークンではありません。if constexpr代わりに使用してください。
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");
}
...
}