开发者

Is it possible to use type_traits to differentiate between char & wchar_t?

I am trying to write a function that can handle both char & wchar_t using the type_traits feature of C++0x. Yes, I know how to do it without type_traits, but I want to do it using type_tra开发者_Python百科its for better understanding of the feature.

template <typename T>
void SplitIt(T* base, T* ext, const std::basic_string<T>& fullFilePath, std::true_type)
{
    _splitpath(fullFilePath.c_str(),NULL,NULL,base,ext);
}

template <typename T>
void SplitIt(T* base, T* ext, const std::basic_string<T>& fullFilePath, std::false_type)
{
    _wsplitpath(fullFilePath.c_str(),NULL,NULL,base,ext);
}

template <typename T>
std::basic_string<T> GetBaseName(const std::basic_string<T>& fullFilePath)
{
    T base[300], ext[50];

    SplitIt(base, ext, fullFilePath, /*What goes here?*/);

    return std::basic_string<T>(buf) + ext;
}


int main()
{
    std::basic_string<TCHAR> baseName = GetBaseName(std::basic_string<TCHAR>(__FILE__));
}

Is there any type_traits property that differentiates char from wchar_t?


I think there's a is_same property, so

SplitIt(base, ext, fullFilePath, is_same<T, char>());

should work.


AFAICS, there is nothing like that in the <type_traits> header. However, it is trivial to make it yourself, you only need to switch the overload since the following now yields std::false_type for char (and everything else) and std::true_type for wchar_t:

#include <type_traits>

template<class T>
struct is_wchar
  : std::false_type
{
};

template<>
struct is_wchar<wchar_t>
  : std::true_type
{
};

// usage:

SplitIt(base, ext, fullFilePath, is_wchar<T>());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜