Skip to content

Instantly share code, notes, and snippets.

@chenshuo
Created September 16, 2012 08:39
Show Gist options
  • Save chenshuo/3731612 to your computer and use it in GitHub Desktop.
Save chenshuo/3731612 to your computer and use it in GitHub Desktop.
char and wchar_t in template
#include <iostream>
template<typename CharT>
const CharT* ToString(const char* str, const wchar_t* wstr);
template<>
const char* ToString<char>(const char* str, const wchar_t* wstr)
{
return str;
}
template<>
const wchar_t* ToString<wchar_t>(const char* str, const wchar_t* wstr)
{
return wstr;
}
#define TOSTRING(T, str) ToString<T>(str, L##str)
int main()
{
std::cout << ToString<char>("Hello", L"World") << std::endl;
std::wcout << ToString<wchar_t>("Hello", L"World") << std::endl;
std::cout << TOSTRING(char, "Who am I") << std::endl;
std::wcout << TOSTRING(wchar_t, "Who am I") << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment