MID
MID

Reputation: 554

Unsigned string construction impossible

I was looking for a way to get a string of unsigned chars. So I stumbled upon this Strings of unsigned chars

So the answer is to create a new typedef as follows :

typedef std::basic_string<unsigned char> ustring;

The thing is, I'm not able to construct with this. when I try :

char *p = "123123";
auto s = ustring(p);

I get the following error :

no instance of constructor "std::basic_string<_Elem, _Traits, 
_Alloc>::basic_string [with _Elem=unsigned char, 
_Traits=std::char_traits<unsigned char>, _Alloc=std::allocator<unsigned 
char>]" matches the argument list

Can someone please shed some light on this issue?

Thanks in advance!

Upvotes: 2

Views: 120

Answers (1)

Bathsheba
Bathsheba

Reputation: 234715

Irrespective of whether or not char is signed or unsigned on your platform, char, unsigned char, and signed char are all distinct types. Rather like int and long are distinct types even if they have the same number of bits and complement scheme.

So you'd have better luck with

const unsigned char* p = (const unsigned char*)"123123";

where I've inserted const qualification to emphasise the read-only nature of the const char[7] literal.

Upvotes: 2

Related Questions