jeudi 9 juillet 2020

Template function to convert std::string in std::wstring and vice_versa with compile time checking

I have the following functions:

std::string convert(const std::wstring &str);
std::wstring convert(const std::string &str);

Which I would like to do something like this:

template<class T, class U> T convert(const U& u);

I created some verification:

template<class T> struct is_string : public std::false_type {};
template<class T> struct is_string<std::basic_string<T>> : public std::true_type {};

And then now I'm confusing on how to do that in a really strict way: if T is std::string, U must be a std::wstring and vice-versa. All other cases shouldn't compile.

Any clue?

My compiler is on C++11

Thanks.

Aucun commentaire:

Enregistrer un commentaire