开发者

Template ToString function that works with different character encoding

I've written an INI class that loads/saves/creates an INI data format, however right now it only works with ascii characters, and I would like to expand it to work with any type of character encoding, so besides char, wchar_t. To do this, I have to setup various string functions for doing the parsing; I have some setup, however I need to rewrite them to work with any type of basic_string.

So, as to my question, I want a ToString function that will work with any type of character encoding, how do I do this?

I h开发者_如何学Pythonave the following two functions:

template <typename T>
static string toStr(const T& val)
{
    stringstream out;
    out << val;
    return out.str();
}

template <typename T>
static wstring toWStr(const T& val)
{
    wstringstream out;
    out << val;
    return out.str();
}


When I originally posted this question I was close to the answer, but made a careless mistake, and thus have solved my own question while I was still typing it. So for those of you who want a generic toString function that works for a variety of string encodings, here you go:

template<typename CharType, typename T>
static basic_string<CharType, char_traits<CharType>, allocator<CharType>> toString(const T& val)
{
    basic_stringstream<CharType> out;
    out << val;
    return out.str();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜