jeudi 17 mai 2018

How to write a template function to create char/wchar_t strings given const char*?

I'd like to write function to create char/wchar_t strings given const char* ('narrow' string literals).

For example, something like

template<typename T>
std::basic_string<T> test(const char* str) {
    ///
}

So that I can use

std::string a = test<char>("haha");

or

std::wstring b = test<wchar_t>("哈哈"); // NOT L"哈哈"

to create strings based on template arguments.

I known it would be easy if the function argument is const T* str. But I can't figure it out when it is const char* str. And I think some conversion must be performed.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire