pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: error: static assertion failed: char should be cast to either signed char or unsigned char

Open mtelka opened this issue 2 years ago • 1 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [ ] Consider asking first in the Gitter chat room or in a Discussion.

What version (or hash if on master) of pybind11 are you using?

2.11.1

Problem description

I'm trying to compile pybind11 tests and compilatoin fails here:

pybind11-2.11.1/tests/test_enum.cpp:95:16: error: static assertion failed: char should be cast to either signed char or unsigned char
   92 |         py::detail::any_of<
      |         ~~~~~~~~~~~~~~~~~~~
   93 |             std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>,  // e.g. gcc on x86
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   94 |             std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char> // e.g. arm linux
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   95 |             >::value,
      |             ~~~^~~~~

I think the assertion is wrong since char, unsigned char and signed char are three distinct types.

Reference: https://en.cppreference.com/w/cpp/language/types https://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

mtelka avatar Nov 11 '23 21:11 mtelka


#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <type_traits>

namespace py = pybind11;

enum class ScopedCharEnum {
    A = 65,
    B = 66
};

PYBIND11_MODULE(test_enum, m) {
    py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
        .value("A", ScopedCharEnum::A)
        .value("B", ScopedCharEnum::B);
}

// In the template code related to the static assertion:
static_assert(
    std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>::value || 
    std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char>::value ||
    std::is_same<py::enum_<ScopedCharEnum>::Scalar, char>::value, // Allow char type as valid
    "char should be cast to either signed char or unsigned char"
);

ljluestc avatar Nov 23 '24 19:11 ljluestc