No UNSIGNED_BYTE in Any

Lubos Lunak l.lunak at suse.cz
Thu Jan 3 05:54:32 PST 2013


On Thursday 27 of December 2012, Lionel Elie Mamane wrote:
> Hi,
>
> The following code does not do what one would think:
>
>  sal_uInt8 nValue = 255;
>  Any aValue;
>  aValue <<= nValue;
>
> aValue now contains a *BOOLEAN*. That's rather... surprising, and
> could bite any of us one day.

 Why could? There's exactly the same problem with sal_Unicode and I'm pretty 
sure there already have been trouble with this.

> But sal_uInt8 and sal_Bool are typedefs of the same type, namely
> "unsigned char", so I don't know how to make the distinction between
> sal_Bool and sal_uInt8 in there.

 You can't. Typedef is an alias for an existing type, not a new type, so 
sal_uInt8 and sal_Bool are exactly the same thing.

> So frankly, I'm not sure what to do...

 How about bother with sal_uInt8, as it doesn't actually gain you anything?

> If we were a 
> pure C++ program, I'd change:
>
> typedef unsigned char sal_Bool;
>
> into
>
> typedef bool sal_Bool;

 This could have been done a long time ago, except that this is breaking 
backwards compatibility, which AFAIK is the reason sal_Bool has to stay what 
it is, despite nobody liking it.

> Since we are in our "unstable API/ABI" period, *if* we can have a
> clean solution, let's have it for LibreOffice 4.1. Since the in-memory
> layout of sal_Bool does not change (right?), this might even be fully
> ABI-compatible? (but not API-compatible for C, but API-compatible for
> C++)

 Turning sal_Bool into a non-integer type breaks enough code to not be worth 
it (I know, I tried it once). If we decide to break ABI, we can just go with 
bool. If not, we probably can't do anything.

> In terms of more hackish solutions, I have thought of not normalising
> sal_Bool in Any (to make them "true" synonyms of sal_uInt8), *but*
> this would break any code of the form:
>
>  if (b0 == sal_True) {}
>  switch (b0)
>  {
>     case sal_False:
>     case sal_True:
>  }

 As already said above, it is a true synonym of sal_uInt8.

> Why did I get into this? Well, see https://gerrit.libreoffice.org/#/c/1164/

 Unless you have _very_ good reasons, just don't bother with 8-bit integers. 
Things like 'signed char', 'unsigned char', 'short' and 'unsigned' are 
ancient stuff from times when the extra few bits of information mattered. 
With some exceptions, nowadays the usually little storage saved by this is 
just not worth the associated trouble.

-- 
 Lubos Lunak
 l.lunak at suse.cz


More information about the LibreOffice mailing list