String << operator missing for unsigned short


#1

I’ve found that an unsigned short value passed to the String operator << ends up treated like a char.
I also think that completing << operators for ‘long’ and ‘unsigned long’ values could be a good idea.


#2

Yes, good request!


#3

Arghhh… Can you revert this change ?
It conflicts with juce_wchar (method already defined) type on my compiler.
wchar == unsigned short when you compile unicode built.

Maybe a define could be good ?


#4

Which compiler is that? Surely it’s perfectly ok to have two methods that take the same sized value if they’re presented as different typedefs… You might need to do an explicit cast when you use them, but I can’t see why it’d fail to compile.


#5

Plain old microsoft Visual C++ 6 compiler.

BTW try this under gcc:

typedef int mytype;
class A
{
   void foo(int) {}
   void foo(mytype) {}  // fatal error: method already defined
};

#6

Hmm - yes, that does fail in gcc too… I always assumed the compiler could distinguish between typedefs like it does other types, but I guess it must just treat them as aliases. That’s a bit lame, really.

Ok, well I’m just going to scrap that << operator, it’s too messy to try to deal with this in any other way. If anyone needs to use it, they can easily just cast their value to an int.


#7

…hang on - in other compilers, juce_wchar is a wchar_t, which is an int. Why doesn’t that clash with the << operator?


#8

… ah, the compiler’s treating it as a special case. I guess that’s why it compiles ok in VC7, where it’s also an unsigned short. Only the crappy old VC6 compiler is actually using a typedef for wchar_t.


#9

using explicit keyword do not solve the issue ?


#10

‘explicit’ is only for use with constructors, I think.


#11

Indeed.


#12

What about the other way, I mean drop the wchar_t version, and provide both int and short int type ?
I guess it’ll work everywhere.


#13

I think being able to append a character to a string is a bit more useful than an unsigned short!


#14

Yes in theory. In practice, as wchar is either an int or a unsigned short, it’ll use whatever overload (int or unsigned short).
Unless I’m mistaken in the operator function, does the “<< operator(wchar)” concatenate the char ?
I initially though it was a convert the integral type to a string version (that is String()<<(wchar)32 == “32” and not " ")


#15