std :: string 및 std :: wstring [duplicate] 모두 트림
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");
}
...
}