Unicode Support for std::getline()
Hey guys, just a quick one.
I've searched around a little and found nothing useful. When I'm coding, I always try to be Unicode-aware, whilst not actually enforcing a particular character set. (Perhaps overcomplicating things this way is not ideal, but I'm trying to get myself into good habits with regards to portability etc.)
Anyway, I'm trying to receive a line of input from the user, into a 'String' typedef (either std::string or std::wstring, depending on an "#ifndef UNICODE" directive), using the std::getline function. The issue lies in the encoding that the standard input is expecting.
Code:
String demostring;
std::getline(std::cin, demostring);
Of course, this won't compile if Unicode is enabled because std::cin is expecting ANSI input. Whereas:
Code:
String demostring;
std::getline(std::wcin, demostring);
will not compile for ANSI builds. (Interestingly enough, I note that up-to-date versions of Visual Studio (Pro) do not support ANSI-only builds. Anyone know why this is? Do Microsoft simply deem such support obsolete now?)
Anyway, what I'm actually trying to achieve can be interpolated from:
Code:
#ifndef UNICODE
typedef std::string String;
typedef std::ifstream Ifstream;
typedef std::cin Cin;
#else
typedef std::wstring String;
typedef std::wifstream Ifstream;
typedef std::wcin Cin;
#endif
Of course, this won't compile, but I think it demonstrates what I'm wanting to do. With the ifstream and wifstream typedefs, I'm wondering if it would be appropriate to initialise an 'Ifstream' and then redirect the standard input to this stream?
This seems like a convoluted way to go about it though; is there perhaps a better solution? I'm aware that I could use an "#ifndef UNICODE" directive to selectively call "std::getline(std::cin, demostring);" or "std::getline(std::wcin, demostring);" within main() itself. However, I'm just wondering if it's possible to accomplish what I'm after without throwing arbitrary preprocessor directives into the body of the code.
And there was me thinking this was going to be a "quick one" :rolleyes:
Many thanks for you time, good people!
Abyssion