Skip to content

Some mpl code does not work when char type is "unsigned char" by default (narrowing) #50

@trex58

Description

@trex58

When compiling libs/mpl/test/char.cpp on AIX with GCC 9.3, where "char" type is "unsigned char" by default, I have the following error:

   "g++"  -fvisibility-inlines-hidden -fPIC -maix32 -pthread -mcmodel=large -O3 -finline-functions -Wno-inline -Wall -fvisibility=hidden -O2 -ffp-contract=off 
   -ftls-model=initial-exec   -DBOOST_ALL_NO_LIB=1 -DBOOST_BEAST_ALLOW_DEPRECATED -DBOOST_BEAST_SEPARATE_COMPILATION -DNDEBUG  -I".." -c
   -o "../bin.v2/libs/mpl/test/char.test/gcc-9/release/threading-multi/visibility-hidden/char.o" "../libs/mpl/test/char.cpp"

In file included from ../boost/mpl/aux_/test/assert.hpp:17,
                from ../boost/mpl/aux_/test.hpp:19,
                from ../libs/mpl/test/integral_wrapper_test.hpp:15,
                from ../libs/mpl/test/char.cpp:17:
../libs/mpl/test/char.cpp: In function 'void test20()':
../libs/mpl/test/char.cpp:22:33: error: narrowing conversion of '-1' from 'int' to 'char' [-Wnarrowing]
  22 | #   define WRAPPER(T, i) char_< i >
     |                                   ^

Looking at details, the exact error is:

./libs/mpl/test/char.cpp.E.cpp: In function 'void test20()':
../libs/mpl/test/char.cpp.E.cpp:30698:162: error: narrowing conversion of '-1' from 'int' to 'char' [-Wnarrowing]
30698 | n_line_23 = sizeof( boost::mpl::assertion_failed<false>( boost::mpl::assert_arg( (void (*) ( is_same< prior< char_<0> >::type, char_<0 -1> > ))0, 1 ) ) ) }; }

where -1 is used for creating a char, which is "unsigned char" on AIX (and on other systems). Thus, creating a char with value -1 is forbidden.

I've already seen this issue on other tests of Boost.
The possible solutions are:

  1. manage "char" so that it works either with char="signed char" or with char="unsigned char". However, since the C/C++ says that the "char" type is "undefined", that depends on how each OS has implemented the details of the char type (like converting a negatif floating point value to an "unsigned" integer/char/short).
  2. replace "char" by "signed char" everywhere.
  3. do not use negative value

Second solution seems to be the more appropriate solution, since it will work everywhere. Unless using negative values is required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions