Created
May 11, 2021 13:29
-
-
Save hidez8891/858a00ae59d1da8681562c48163cbdc9 to your computer and use it in GitHub Desktop.
templateパラメータによる文字リテラルの変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <tuple> | |
#include <boost/preprocessor/cat.hpp> | |
#define literal_cast(T, CHARS) \ | |
std::get<const T*>( \ | |
std::tuple< \ | |
const char*, \ | |
const wchar_t* \ | |
>( \ | |
CHARS, \ | |
BOOST_PP_CAT(L, CHARS) \ | |
)) | |
template <typename CharT> | |
void trim(std::basic_string<CharT> &s, const CharT *chars = literal_cast(CharT, " \r\n\t\v")) | |
{ | |
s.erase(0, s.find_first_not_of(chars)); | |
s.erase(s.find_last_not_of(chars)+1); | |
} | |
int main() | |
{ | |
{ | |
std::string str{ " ABC " }; | |
trim(str); | |
std::cout << str << std::endl; | |
} | |
{ | |
std::wstring wstr{ L" ABC " }; | |
trim(wstr); | |
std::wcout << wstr << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment