I tried to specialize basic_ostream and basic_iostream for the new character types like this in the <iostream> header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
typedef basic_ostream<char16_t> u16ostream;
typedef basic_ostream<char32_t> u32ostream;
typedef basic_istream<char16_t> u16istream;
typedef basic_istream<char32_t> u32istream;
extern u16istream u16cin; /// Linked to standard input
extern u16ostream u16cout; /// Linked to standard output
extern u16ostream u16cerr; /// Linked to standard error (unbuffered)
extern u16ostream u16clog; /// Linked to standard error (buffered)
extern u32istream u32cin; /// Linked to standard input
extern u32ostream u32cout; /// Linked to standard output
extern u32ostream u32cerr; /// Linked to standard error (unbuffered)
extern u32ostream u32clog; /// Linked to standard error (buffered)
But it reports linker errors. Is there something I'm missing, or is it simply impossible for now?
When you use extern you say that that variable is global and shared by all the sources of your project.
However there must be one that instantiate it. You need to put u16istream u16cin; //any constructor that you like in one cpp
But what constructor? I can't use the default or the copy constructor because they are private, so I can only use the one that takes a streambuf*. But then how to make that streambuf? It has NO public constructors! Absolutely none! So how am I supposed to do that?
You cannot use streambuf directly. Use filebuf or stringbuf
This is an abstract base class, therefore no objects can be directly instantiated from it. The standard hierarchy defines two classes derived from this one that can be used to directly instantiate objects: filebuf and stringbuf.
but to achieve that it behaves like the original cin/cout you have to make your own streambuf. It's not impossible...
And it works and prints UTF-16 characters perfectly! But it's only because wchar_t has the same size as char16_t... I don't know how I'm gonna do it for char32_t...
But it's only because wchar_t has the same size as char16_t
Which is actually a Windows defect: wchar_t is intended to be able to represent every code point, but it doesn't on Windows. wchar_t is 32-bit on other systems.
As for wrapping cin/cout, that's what std::wbuffer_convert is for.
Why does it throw bad_cast in an input operation? I highly doubt there is a dynamic_cast somewhere in the extraction operator definition, so why does it happen??
I don't know! u16cout, u16cerr and u16clog all work with no problem! And the problem is in *some* dynamic_cast my u16cin is supposed to run, but I don't know where, when or how!
C:\Users\Vilim\Desktop\Nova mapa\Neimenovano1.cpp In function 'int main()':
5 C:\Users\Vilim\Desktop\Nova mapa\Neimenovano1.cpp:9 'wbuffer_convert' was not declared in this scope
21 C:\Users\Vilim\Desktop\Nova mapa\Neimenovano1.cpp:9 'codecvt_utf8_utf16' was not declared in this scope
40 C:\Users\Vilim\Desktop\Nova mapa\Neimenovano1.cpp:9 expected primary-expression before 'char16_t'
40 C:\Users\Vilim\Desktop\Nova mapa\Neimenovano1.cpp:9 expected ';' before 'char16_t'