Skip to content

Instantly share code, notes, and snippets.

@hidez8891
Created May 11, 2021 13:29
Show Gist options
  • Save hidez8891/858a00ae59d1da8681562c48163cbdc9 to your computer and use it in GitHub Desktop.
Save hidez8891/858a00ae59d1da8681562c48163cbdc9 to your computer and use it in GitHub Desktop.
templateパラメータによる文字リテラルの変換
#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