Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

typetraits

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_traits 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?

like image 553
Sharath Avatar asked Jul 07 '11 11:07

Sharath


2 Answers

I think there's a is_same property, so

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

should work.

like image 135
MartinStettner Avatar answered Sep 20 '22 10:09

MartinStettner


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>());
like image 42
Xeo Avatar answered Sep 19 '22 10:09

Xeo