c++ - template specialization for wchar_t -


can explain why doesn't work?

template < typename t > struct tester { static const size_t value = 0; }; template <> struct tester< char > { static const size_t value = 1; }; template <> struct tester< unsigned short > { static const size_t value = 2; };  size_t ntest = tester< wchar_t >::value; 

on compiler, wchar_t typedef'd unsigned short. why default template being used when underlying type has specialization?

edit: ok, wrong being type defined. intellisense showing me else. cross platform, , surrogate, question remains though.

this has thrown me curveball because want work wchar_t depending on it's size.

another related question. how can work wchar_t in cross platform manner? know it's 16 bits on windows, , elsewhere can 32 bits. if it's defined 32 bit type mean doesn't (as in compiler forced) use surrogate pairs?

would work?

template < typename t, size_t n = sizeof( wchar_t ) > struct remap; template <> struct remap< wchar_t, 2 > { typedef unsigned short type; }; template <> struct remap< wchar_t, 4 > { typedef unsigned long type; }; 

the c++ standard specifies wchar_t unique type , not typedef. on non-conforming implementations, or implementation-defined option, may typedef, cannot rely on or rely on being typedef'd particular type in portable code.

yes, remap specializations work; remap<wchar_t>::type unsigned long on platforms 4 byte wchar_t , unsigned short on platforms 2 byte wchar_t. of course wchar_t not limited 2 sizes, , 2 byte value doesn't mean it's 16 bits, etc. if want write portable code based on largest value wchar_t can hold might @ wchar_max or 1 of options shown in comments, rather sizeof(wchar_t).

if it's defined 32 bit type mean doesn't (as in compiler forced) use surrogate pairs?

the standard doesn't specify wchar_t in way that's particularly useful things people want use for. intent of wchar_t provide type character in current locale represented single wchar_t value, in order enable easier text processing. such wchar_t 1) isn't required use same encoding in locales , 2) surrogate pairs aren't permitted.

windows' use of utf-16 sort of gets around seconds point not supporting locale contain characters require surrogate pairs. means portable code won't deal surrogate pairs on platform, including windows.

but yes, on platforms 32 bit wide wchar_t utf-32 common wchar_t encoding many locales, if stick locales use utf-8 char encoding.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -