Tiny request: size_t constructor for String


#1

Lots of things in this world return a size_t - but if you try to make a String out of one you get the usual error: error: call of overloaded 'String(size_t)' is ambiguous. I can fix this by casting to e.g. an int but it’d be nice to get the size_t constructor in there as well, unless there’s some difficulty with this…


#2

Yes, good request. Can’t think of any reason why not!


#3

…ah no, I just remembered the reason why not. I have attempted this before.

Some compilers don’t treat size_t as a separate type from other integer types, so you can’t overload it if String (unsigned int) is also defined. If it was just one compiler, it’d be easy to work around, but it’s not just MSVC that’s broken, it’s also GCC on certain platforms like Android… Very hard to try to build a #ifdef that’ll correctly detect the compilers and platforms that can handle this and those that can’t.


#4

Later, I thought that might be the issue.

But I’m a tiny bit baffled. String does seem to have an uint64 constructor - and size_t is unsigned long - aren’t they the same?


#5

[quote=“TomSwirly”]Later, I thought that might be the issue.

But I’m a tiny bit baffled. String does seem to have an uint64 constructor - and size_t is unsigned long - aren’t they the same?[/quote]

Some compilers see them as the same type, others as being different. That’s what makes it awkward…


#6

I knew that the definition of size_t was different on different compilers.

But on whatever compiler runs on XCode 3, I drilled down to find that, apparently, size_t was unsigned long, so I’m baffled your constructor isn’t matching…

Whatevs. This is picayune in the Grand Scale Of Things! Frankly, in the best possible world I don’t think these should be constructors anyway, but static helper functions (so there wouldn’t be an issue, you could have a separate String::fromSizeT) - but none of us want you fiddling around with tiny things when you could be off writing fancy audio or graphics stuff… :smiley: