Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)

// forward declaration (definition in pybind11.h)
template <typename T>
std::string generate_type_signature();
PYBIND11_CONSTEVAL std::string generate_type_signature();

// Declared in pytypes.h:
template <typename T, enable_if_t<!is_pyobject<T>::value, int>>
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@
# define PYBIND11_DTOR_CONSTEXPR
#endif

#if defined(__cpp_consteval)
# define PYBIND11_CONSTEVAL consteval
# define PYBIND11_CONSTEVAL_OR_CONSTEXPR consteval
#else
# define PYBIND11_CONSTEVAL
# define PYBIND11_CONSTEVAL_OR_CONSTEXPR constexpr
#endif

// Compiler version assertions
#if defined(__INTEL_COMPILER)
# if __INTEL_COMPILER < 1800
Expand Down
76 changes: 38 additions & 38 deletions include/pybind11/detail/descr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ template <size_t N, typename... Ts>
struct descr {
char text[N + 1]{'\0'};

constexpr descr() = default;
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr() = default;
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr descr(char const (&s)[N + 1]) : descr(s, make_index_sequence<N>()) {}
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr(char const (&s)[N + 1]) : descr(s, make_index_sequence<N>()) {}

template <size_t... Is>
constexpr descr(char const (&s)[N + 1], index_sequence<Is...>) : text{s[Is]..., '\0'} {}
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr(char const (&s)[N + 1], index_sequence<Is...>) : text{s[Is]..., '\0'} {}

template <typename... Chars>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr descr(char c, Chars... cs) : text{c, static_cast<char>(cs)..., '\0'} {}
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr(char c, Chars... cs) : text{c, static_cast<char>(cs)..., '\0'} {}

static constexpr std::array<const std::type_info *, sizeof...(Ts) + 1> types() {
static PYBIND11_CONSTEVAL_OR_CONSTEXPR std::array<const std::type_info *, sizeof...(Ts) + 1> types() {
return {{&typeid(Ts)..., nullptr}};
}
};

template <size_t N1, size_t N2, typename... Ts1, typename... Ts2, size_t... Is1, size_t... Is2>
constexpr descr<N1 + N2, Ts1..., Ts2...> plus_impl(const descr<N1, Ts1...> &a,
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N1 + N2, Ts1..., Ts2...> plus_impl(const descr<N1, Ts1...> &a,
const descr<N2, Ts2...> &b,
index_sequence<Is1...>,
index_sequence<Is2...>) {
Expand All @@ -51,16 +51,16 @@ constexpr descr<N1 + N2, Ts1..., Ts2...> plus_impl(const descr<N1, Ts1...> &a,
}

template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a,
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a,
const descr<N2, Ts2...> &b) {
return plus_impl(a, b, make_index_sequence<N1>(), make_index_sequence<N2>());
}

template <size_t N>
constexpr descr<N - 1> const_name(char const (&text)[N]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N - 1> const_name(char const (&text)[N]) {
return descr<N - 1>(text);
}
constexpr descr<0> const_name(char const (&)[1]) { return {}; }
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<0> const_name(char const (&)[1]) { return {}; }

template <size_t Rem, size_t... Digits>
struct int_to_str : int_to_str<Rem / 10, Rem % 10, Digits...> {};
Expand All @@ -72,49 +72,49 @@ struct int_to_str<0, Digits...> {

// Ternary description (like std::conditional)
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<B, descr<N1 - 1>> const_name(char const (&text1)[N1], char const (&)[N2]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<B, descr<N1 - 1>> const_name(char const (&text1)[N1], char const (&)[N2]) {
return const_name(text1);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<!B, descr<N2 - 1>> const_name(char const (&)[N1], char const (&text2)[N2]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<!B, descr<N2 - 1>> const_name(char const (&)[N1], char const (&text2)[N2]) {
return const_name(text2);
}

template <bool B, typename T1, typename T2>
constexpr enable_if_t<B, T1> const_name(const T1 &d, const T2 &) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<B, T1> const_name(const T1 &d, const T2 &) {
return d;
}
template <bool B, typename T1, typename T2>
constexpr enable_if_t<!B, T2> const_name(const T1 &, const T2 &d) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<!B, T2> const_name(const T1 &, const T2 &d) {
return d;
}

template <size_t Size>
auto constexpr const_name() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
auto PYBIND11_CONSTEVAL_OR_CONSTEXPR const_name() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
return int_to_str<Size / 10, Size % 10>::digits;
}

template <typename Type>
constexpr descr<1, Type> const_name() {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<1, Type> const_name() {
return {'%'};
}

// Use a different name based on whether the parameter is used as input or output
template <size_t N1, size_t N2>
constexpr descr<N1 + N2 + 1> io_name(char const (&text1)[N1], char const (&text2)[N2]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N1 + N2 + 1> io_name(char const (&text1)[N1], char const (&text2)[N2]) {
return const_name("@") + const_name(text1) + const_name("@") + const_name(text2)
+ const_name("@");
}

// Ternary description for io_name (like the numeric type_caster)
template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<B, descr<N1 + N2 + 1>>
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<B, descr<N1 + N2 + 1>>
io_name(char const (&text1)[N1], char const (&text2)[N2], char const (&)[N3], char const (&)[N4]) {
return io_name(text1, text2);
}

template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<!B, descr<N3 + N4 + 1>>
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<!B, descr<N3 + N4 + 1>>
io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char const (&text4)[N4]) {
return io_name(text3, text4);
}
Expand All @@ -126,99 +126,99 @@ io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char co
#ifndef _
# define PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY
template <size_t N>
constexpr descr<N - 1> _(char const (&text)[N]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N - 1> _(char const (&text)[N]) {
return const_name<N>(text);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<B, descr<N1 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<B, descr<N1 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
return const_name<B, N1, N2>(text1, text2);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<!B, descr<N2 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<!B, descr<N2 - 1>> _(char const (&text1)[N1], char const (&text2)[N2]) {
return const_name<B, N1, N2>(text1, text2);
}
template <bool B, typename T1, typename T2>
constexpr enable_if_t<B, T1> _(const T1 &d1, const T2 &d2) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<B, T1> _(const T1 &d1, const T2 &d2) {
return const_name<B, T1, T2>(d1, d2);
}
template <bool B, typename T1, typename T2>
constexpr enable_if_t<!B, T2> _(const T1 &d1, const T2 &d2) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR enable_if_t<!B, T2> _(const T1 &d1, const T2 &d2) {
return const_name<B, T1, T2>(d1, d2);
}

template <size_t Size>
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
auto PYBIND11_CONSTEVAL_OR_CONSTEXPR _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
return const_name<Size>();
}
template <typename Type>
constexpr descr<1, Type> _() {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<1, Type> _() {
return const_name<Type>();
}
#endif // #ifndef _

constexpr descr<0> concat() { return {}; }
constexpr descr<0> union_concat() { return {}; }
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<0> concat() { return {}; }
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<0> union_concat() { return {}; }

template <size_t N, typename... Ts>
constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N, Ts...> concat(const descr<N, Ts...> &descr) {
return descr;
}

template <size_t N, typename... Ts>
constexpr descr<N, Ts...> union_concat(const descr<N, Ts...> &descr) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N, Ts...> union_concat(const descr<N, Ts...> &descr) {
return descr;
}

template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
constexpr descr<N1 + N2 + 3, Ts1..., Ts2...> operator|(const descr<N1, Ts1...> &a,
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N1 + N2 + 3, Ts1..., Ts2...> operator|(const descr<N1, Ts1...> &a,
const descr<N2, Ts2...> &b) {
return a + const_name(" | ") + b;
}

#ifdef __cpp_fold_expressions
template <size_t N1, size_t N2, typename... Ts1, typename... Ts2>
constexpr descr<N1 + N2 + 2, Ts1..., Ts2...> operator,(const descr<N1, Ts1...> &a,
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N1 + N2 + 2, Ts1..., Ts2...> operator,(const descr<N1, Ts1...> &a,
const descr<N2, Ts2...> &b) {
return a + const_name(", ") + b;
}

template <size_t N, typename... Ts, typename... Args>
constexpr auto concat(const descr<N, Ts...> &d, const Args &...args) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR auto concat(const descr<N, Ts...> &d, const Args &...args) {
return (d, ..., args);
}

template <size_t N, typename... Ts, typename... Args>
constexpr auto union_concat(const descr<N, Ts...> &d, const Args &...args) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR auto union_concat(const descr<N, Ts...> &d, const Args &...args) {
return (d | ... | args);
}

#else
template <size_t N, typename... Ts, typename... Args>
constexpr auto concat(const descr<N, Ts...> &d, const Args &...args)
PYBIND11_CONSTEVAL_OR_CONSTEXPR auto concat(const descr<N, Ts...> &d, const Args &...args)
-> decltype(std::declval<descr<N + 2, Ts...>>() + concat(args...)) {
return d + const_name(", ") + concat(args...);
}

template <size_t N, typename... Ts, typename... Args>
constexpr auto union_concat(const descr<N, Ts...> &d, const Args &...args)
PYBIND11_CONSTEVAL_OR_CONSTEXPR auto union_concat(const descr<N, Ts...> &d, const Args &...args)
-> decltype(std::declval<descr<N + 3, Ts...>>() + union_concat(args...)) {
return d + const_name(" | ") + union_concat(args...);
}

#endif

template <size_t N, typename... Ts>
constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
return const_name("{") + descr + const_name("}");
}

template <size_t N, typename... Ts>
constexpr descr<N + 4, Ts...> arg_descr(const descr<N, Ts...> &descr) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N + 4, Ts...> arg_descr(const descr<N, Ts...> &descr) {
return const_name("@^") + descr + const_name("@!");
}

template <size_t N, typename... Ts>
constexpr descr<N + 4, Ts...> return_descr(const descr<N, Ts...> &descr) {
PYBIND11_CONSTEVAL_OR_CONSTEXPR descr<N + 4, Ts...> return_descr(const descr<N, Ts...> &descr) {
return const_name("@$") + descr + const_name("@!");
}

Expand Down
6 changes: 3 additions & 3 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ inline std::string replace_newlines_and_squash(const char *text) {
}

/* Generate a proper function signature */
inline std::string generate_function_signature(const char *type_caster_name_field,
PYBIND11_DTOR_CONSTEXPR inline std::string generate_function_signature(const char *type_caster_name_field,
detail::function_record *func_rec,
const std::type_info *const *types,
size_t &type_index,
Expand Down Expand Up @@ -230,8 +230,8 @@ inline std::string generate_function_signature(const char *type_caster_name_fiel
}

template <typename T>
inline std::string generate_type_signature() {
static constexpr auto caster_name_field = make_caster<T>::name;
PYBIND11_CONSTEVAL inline std::string generate_type_signature() {
constexpr auto caster_name_field = make_caster<T>::name;
PYBIND11_DESCR_CONSTEXPR auto descr_types = decltype(caster_name_field)::types();
// Create a default function_record to ensure the function signature has the proper
// configuration e.g. no_convert.
Expand Down
Loading