Fully simulating missing individual built-in types (in particular: char16_t and char32_t)

C ++ 11 has two new data types with integer characters, char16_tand char32_t. I would like to simulate them for compilers that do not have a specific type to overload I / O operations to see them as characters instead of their integer value.

These are the following requirements:

  • Distinctive (no typedef).
  • exact width on normal systems (ala uint16_t and uint32_t)
  • other C ++ 11 features are allowed (see first try below)
  • Must play with literals well; char16_t c16 = u"blabla unicode text blabla";must work.
  • If char16_t can be used in mathematical operators, obviously this should work as well.

My first attempt, which is not performed in the literal department, is a strongly typed enumeration:

enum char16_t : uint16_t;

This has other drawbacks that can possibly be solved by supplying the necessary operators on their own (which is really fine for me).

+5
source share
1 answer

I do not think that you will earn initialization, because she has few opportunities for her work. The problem is that the initialization you use in your example should not work: the string literal u"..."gives an array of objects char16_t const, and you want to initialize it with a pointer:

char16_t const* c16 = u"...";

, char16_t char16_t. , , -, . , , , char16_t, , char16_t. , 16 , wchar_t 32 .

#define CONCAT(a,b) a##b

#if defined(HAS_C16)
#  define C16S(s) CONCAT(u,s)
#else
#  define C16S(s) reinterpret_cast<char16_t const*>(CONCAT(L,s));
struct char16_t
{
    unsigned short value;
};
#endif


int main()
{
    char16_t const* c16 = C16S("...");
}

, , . .

+1

All Articles