Skip to content

type_traits_test fails to compile on platforms where char is unsigned #191

@singhc7

Description

@singhc7

Summary

test/unittest/type_traits_test.cpp contains a static_assert that assumes char is signed. This fails to compile on platforms where char is unsigned by default, such as aarch64-linux, ppc64, and several embedded targets.

Affected line

https://github.com/atcoder/ac-library/blob/v1.6/test/unittest/type_traits_test.cpp#L47

static_assert(is_same<unsigned char, internal::to_unsigned_t<char>>::value, "");

This holds when char is signed (e.g. x86_64-linux), because to_unsigned_t<char> resolves to unsigned char. On platforms where char is unsigned, internal::is_signed_int<char> is false, so to_unsigned_t<char> resolves to char itself — not unsigned char — and the assertion fails.

Reproduction

Build the unit tests on any platform where char is unsigned, or force it on x86_64:

g++ -funsigned-char -std=c++14 -I. test/unittest/type_traits_test.cpp -c

Output:

type_traits_test.cpp:47:70: error: static assertion failed
   47 | static_assert(is_same<unsigned char, internal::to_unsigned_t<char>>::value, "");
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~

Suggested fix

Guard the assertion with CHAR_MIN < 0 so the correct relationship is checked on each platform:

#if CHAR_MIN < 0
static_assert(is_same<unsigned char, internal::to_unsigned_t<char>>::value, "");
#else
static_assert(is_same<char, internal::to_unsigned_t<char>>::value, "");
#endif

(<climits> needs to be included.)

The library itself works correctly on these platforms — only this one test assertion is incorrect. PR coming.

Environment

  • Compiler: GCC 15.2.0
  • Platform: aarch64-linux
  • Standard: C++14 / C++17 (both reproduce)

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