這篇文章擴展以前的
http://blog.csdn.net/u012442719/article/details/50827771
在unicode模式下,如何同時接受寬字符和窄字符,並且接受不同變量的debug輸出:
#include<windows.h>
#include<iostream>
#include<string>
#include<cassert>
//first;
template<typename T>
class IsWchar;
template<>
class IsWchar<char>
{
public:
static const int theType = 0;
};
template<>
class IsWchar<char*>
{
public:
static const int theType = 0;
};
template<>
class IsWchar<const char*>
{
public:
static const int theType = 0;
};
template<>
class IsWchar<wchar_t>
{
public:
static const int theType = 1;
};
template<>
class IsWchar<const wchar_t>
{
public:
static const int theType = 1;
};
template<>
class IsWchar<wchar_t*>
{
public:
static const int theType = 1;
};
template<>
class IsWchar<const wchar_t*>
{
public:
static const int theType = 1;
};
//second;
template<int num,typename vT>
class Decision;
template<typename vT>
class Decision<0, vT>
{
public:
static void PTSTR(vT var)
{
int length=strlen(var);
assert(length != 0);
wchar_t* pWchar = new wchar_t[(length + 1)*2];
for (int i = 0; i < length;++i)
{
pWchar[i] = var[i];
}
pWchar[length] = L'\0';
OutputDebugString(pWchar);
delete[] pWchar;
}
};
template<typename vT>
class Decision<1, vT>
{
public:
static void PTSTR(vT var)
{
OutputDebugString(var);
}
};
//3
template<typename vT>
void debugPrint(vT string_)
{
Decision<IsWchar<vT>::theType, vT>::PTSTR(string_);
OutputDebugString(L"\n");
}
template<typename vT,typename T2>
void debugPrint(vT string_,T2 var)
{
Decision<IsWchar<vT>::theType, vT>::PTSTR(string_);
OutputDebugString(std::to_wstring(var).c_str());
OutputDebugString(L"\n");
}
int main()
{
debugPrint("aaa");
debugPrint(L"bbb");
debugPrint("something ", 15);
debugPrint(L"something ", 16.2);
getchar();
return 0;
}
效果如下: