class P, typename Default>
+struct exactly_one {
+ using type = Default;
+};
+
+template class Predicate, typename Default, typename... Ts>
+using exactly_one_t = typename exactly_one::type;
+
+/// Defer the evaluation of type T until types Us are instantiated
+template
+struct deferred_type {
+ using type = T;
+};
+template
+using deferred_t = typename deferred_type::type;
+
+/// Like is_base_of, but requires a strict base (i.e. `is_strict_base_of::value == false`,
+/// unlike `std::is_base_of`)
+template
+using is_strict_base_of
+ = bool_constant::value && !std::is_same::value>;
+
+/// Like is_base_of, but also requires that the base type is accessible (i.e. that a Derived
+/// pointer can be converted to a Base pointer) For unions, `is_base_of::value` is False, so
+/// we need to check `is_same` as well.
+template
+using is_accessible_base_of
+ = bool_constant<(std::is_same::value || std::is_base_of::value)
+ && std::is_convertible::value>;
+
+template class Base>
+struct is_template_base_of_impl {
+ template
+ static std::true_type check(Base *);
+ static std::false_type check(...);
+};
+
+/// Check if a template is the base of a type. For example:
+/// `is_template_base_of` is true if `struct T : Base {}` where U can be anything
+template class Base, typename T>
+// Sadly, all MSVC versions incl. 2022 need the workaround, even in C++20 mode.
+// See also: https://github.com/pybind/pybind11/pull/3741
+#if !defined(_MSC_VER)
+using is_template_base_of
+ = decltype(is_template_base_of_impl::check((intrinsic_t *) nullptr));
+#else
+struct is_template_base_of
+ : decltype(is_template_base_of_impl::check((intrinsic_t *) nullptr)){};
+#endif
+
+/// Check if T is an instantiation of the template `Class`. For example:
+/// `is_instantiation` is true if `T == shared_ptr` where U can be anything.
+template class Class, typename T>
+struct is_instantiation : std::false_type {};
+template class Class, typename... Us>
+struct is_instantiation> : std::true_type {};
+
+/// Check if T is std::shared_ptr where U can be anything
+template
+using is_shared_ptr = is_instantiation;
+
+/// Check if T looks like an input iterator
+template
+struct is_input_iterator : std::false_type {};
+template
+struct is_input_iterator()), decltype(++std::declval())>>
+ : std::true_type {};
+
+template
+using is_function_pointer
+ = bool_constant::value
+ && std::is_function::type>::value>;
+
+template
+struct strip_function_object {
+ // If you are encountering an
+ // 'error: name followed by "::" must be a class or namespace name'
+ // with the Intel compiler and a noexcept function here,
+ // try to use noexcept(true) instead of plain noexcept.
+ using type = typename remove_class::type;
+};
+
+// Extracts the function signature from a function, function pointer or lambda.
+template >
+using function_signature_t = conditional_t<
+ std::is_function::value,
+ F,
+ typename conditional_t::value || std::is_member_pointer::value,
+ std::remove_pointer,
+ strip_function_object>::type>;
+
+/// Returns true if the type looks like a lambda: that is, isn't a function, pointer or member
+/// pointer. Note that this can catch all sorts of other things, too; this is intended to be used
+/// in a place where passing a lambda makes sense.
+template
+using is_lambda = satisfies_none_of,
+ std::is_function,
+ std::is_pointer,
+ std::is_member_pointer>;
+
+// [workaround(intel)] Internal error on fold expression
+/// Apply a function over each element of a parameter pack
+#if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER)
+// Intel compiler produces an internal error on this fold expression (tested with ICC 19.0.2)
+# define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...)
+#else
+using expand_side_effects = bool[];
+# define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) \
+ (void) pybind11::detail::expand_side_effects { ((PATTERN), void(), false)..., false }
+#endif
+
+PYBIND11_NAMESPACE_END(detail)
+
+/// C++ bindings of builtin Python exceptions
+class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error {
+public:
+ using std::runtime_error::runtime_error;
+ /// Set the error using the Python C API
+ virtual void set_error() const = 0;
+};
+
+#define PYBIND11_RUNTIME_EXCEPTION(name, type) \
+ class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { \
+ public: \
+ using builtin_exception::builtin_exception; \
+ name() : name("") {} \
+ void set_error() const override { PyErr_SetString(type, what()); } \
+ };
+
+PYBIND11_RUNTIME_EXCEPTION(stop_iteration, PyExc_StopIteration)
+PYBIND11_RUNTIME_EXCEPTION(index_error, PyExc_IndexError)
+PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError)
+PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError)
+PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError)
+PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError)
+PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError)
+PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError)
+PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or
+ /// handle::call fail due to a type
+ /// casting error
+PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally
+
+[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason) {
+ assert(!PyErr_Occurred());
+ throw std::runtime_error(reason);
+}
+[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const std::string &reason) {
+ assert(!PyErr_Occurred());
+ throw std::runtime_error(reason);
+}
+
+template
+struct format_descriptor {};
+
+template
+struct format_descriptor<
+ T,
+ detail::enable_if_t::value>> {
+ static constexpr const char c = 'O';
+ static constexpr const char value[2] = {c, '\0'};
+ static std::string format() { return std::string(1, c); }
+};
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+// Returns the index of the given type in the type char array below, and in the list in numpy.h
+// The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double;
+// complex float,double,long double. Note that the long double types only participate when long
+// double is actually longer than double (it isn't under MSVC).
+// NB: not only the string below but also complex.h and numpy.h rely on this order.
+template
+struct is_fmt_numeric {
+ static constexpr bool value = false;
+};
+template
+struct is_fmt_numeric::value>> {
+ static constexpr bool value = true;
+ static constexpr int index
+ = std::is_same::value
+ ? 0
+ : 1
+ + (std::is_integral::value
+ ? detail::log2(sizeof(T)) * 2 + std::is_unsigned::value
+ : 8
+ + (std::is_same::value ? 1
+ : std::is_same::value ? 2
+ : 0));
+};
+PYBIND11_NAMESPACE_END(detail)
+
+template
+struct format_descriptor::value>> {
+ static constexpr const char c = "?bBhHiIqQfdg"[detail::is_fmt_numeric::index];
+ static constexpr const char value[2] = {c, '\0'};
+ static std::string format() { return std::string(1, c); }
+};
+
+#if !defined(PYBIND11_CPP17)
+
+template
+constexpr const char
+ format_descriptor::value>>::value[2];
+
+#endif
+
+/// RAII wrapper that temporarily clears any Python error state
+struct error_scope {
+ PyObject *type, *value, *trace;
+ error_scope() { PyErr_Fetch(&type, &value, &trace); }
+ error_scope(const error_scope &) = delete;
+ error_scope &operator=(const error_scope &) = delete;
+ ~error_scope() { PyErr_Restore(type, value, trace); }
+};
+
+/// Dummy destructor wrapper that can be used to expose classes with a private destructor
+struct nodelete {
+ template
+ void operator()(T *) {}
+};
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+template
+struct overload_cast_impl {
+ template
+ constexpr auto operator()(Return (*pf)(Args...)) const noexcept -> decltype(pf) {
+ return pf;
+ }
+
+ template
+ constexpr auto operator()(Return (Class::*pmf)(Args...),
+ std::false_type = {}) const noexcept -> decltype(pmf) {
+ return pmf;
+ }
+
+ template
+ constexpr auto operator()(Return (Class::*pmf)(Args...) const,
+ std::true_type) const noexcept -> decltype(pmf) {
+ return pmf;
+ }
+};
+PYBIND11_NAMESPACE_END(detail)
+
+// overload_cast requires variable templates: C++14
+#if defined(PYBIND11_CPP14)
+# define PYBIND11_OVERLOAD_CAST 1
+/// Syntax sugar for resolving overloaded function pointers:
+/// - regular: static_cast(&Class::func)
+/// - sweet: overload_cast(&Class::func)
+template
+static constexpr detail::overload_cast_impl overload_cast{};
+#endif
+
+/// Const member function selector for overload_cast
+/// - regular: static_cast(&Class::func)
+/// - sweet: overload_cast(&Class::func, const_)
+static constexpr auto const_ = std::true_type{};
+
+#if !defined(PYBIND11_CPP14) // no overload_cast: providing something that static_assert-fails:
+template
+struct overload_cast {
+ static_assert(detail::deferred_t::value,
+ "pybind11::overload_cast<...> requires compiling in C++14 mode");
+};
+#endif // overload_cast
+
+PYBIND11_NAMESPACE_BEGIN(detail)
+
+// Adaptor for converting arbitrary container arguments into a vector; implicitly convertible from
+// any standard container (or C-style array) supporting std::begin/std::end, any singleton
+// arithmetic type (if T is arithmetic), or explicitly constructible from an iterator pair.
+template
+class any_container {
+ std::vector v;
+
+public:
+ any_container() = default;
+
+ // Can construct from a pair of iterators
+ template ::value>>
+ any_container(It first, It last) : v(first, last) {}
+
+ // Implicit conversion constructor from any arbitrary container type
+ // with values convertible to T
+ template ())),
+ T>::value>>
+ // NOLINTNEXTLINE(google-explicit-constructor)
+ any_container(const Container &c) : any_container(std::begin(c), std::end(c)) {}
+
+ // initializer_list's aren't deducible, so don't get matched by the above template;
+ // we need this to explicitly allow implicit conversion from one:
+ template ::value>>
+ any_container(const std::initializer_list &c) : any_container(c.begin(), c.end()) {}
+
+ // Avoid copying if given an rvalue vector of the correct type.
+ // NOLINTNEXTLINE(google-explicit-constructor)
+ any_container(std::vector &&v) : v(std::move(v)) {}
+
+ // Moves the vector out of an rvalue any_container
+ // NOLINTNEXTLINE(google-explicit-constructor)
+ operator std::vector &&() && { return std::move(v); }
+
+ // Dereferencing obtains a reference to the underlying vector
+ std::vector &operator*() { return v; }
+ const std::vector &operator*() const { return v; }
+
+ // -> lets you call methods on the underlying vector
+ std::vector *operator->() { return &v; }
+ const std::vector *operator->() const { return &v; }
+};
+
+// Forward-declaration; see detail/class.h
+std::string get_fully_qualified_tp_name(PyTypeObject *);
+
+template
+inline static std::shared_ptr
+try_get_shared_from_this(std::enable_shared_from_this *holder_value_ptr) {
+// Pre C++17, this code path exploits undefined behavior, but is known to work on many platforms.
+// Use at your own risk!
+// See also https://en.cppreference.com/w/cpp/memory/enable_shared_from_this, and in particular
+// the `std::shared_ptr gp1 = not_so_good.getptr();` and `try`-`catch` parts of the example.
+#if defined(__cpp_lib_enable_shared_from_this) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
+ return holder_value_ptr->weak_from_this().lock();
+#else
+ try {
+ return holder_value_ptr->shared_from_this();
+ } catch (const std::bad_weak_ptr &) {
+ return nullptr;
+ }
+#endif
+}
+
+// For silencing "unused" compiler warnings in special situations.
+template