diff --git a/include/pybind11/detail/type_caster_odr_guard.h b/include/pybind11/detail/type_caster_odr_guard.h index b8bf87453..267461a86 100644 --- a/include/pybind11/detail/type_caster_odr_guard.h +++ b/include/pybind11/detail/type_caster_odr_guard.h @@ -112,6 +112,28 @@ struct tu_local_no_data_always_false { } // namespace +# ifndef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT +# define PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT +# endif + +template +struct get_type_caster_source_file_line { +# ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT + static_assert(TypeCasterType::source_file_line, + "PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE is MISSING: Please add that macro to the " + "TypeCasterType, or undefine PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT"); +# else + static constexpr auto source_file_line = tu_local_const_name("UNAVAILABLE"); +# endif +}; + +template +struct get_type_caster_source_file_line< + TypeCasterType, + enable_if_t::value>> { + static constexpr auto source_file_line = TypeCasterType::source_file_line; +}; + template struct type_caster_odr_guard : TypeCasterType { static tu_local_no_data_always_false translation_unit_local; @@ -121,9 +143,10 @@ template tu_local_no_data_always_false type_caster_odr_guard::translation_unit_local = []() { - type_caster_odr_guard_impl(typeid(IntrinsicType), - TypeCasterType::source_file_line.text, - PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED); + type_caster_odr_guard_impl( + typeid(IntrinsicType), + get_type_caster_source_file_line::source_file_line.text, + PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_IMPL_THROW_DISABLED); return tu_local_no_data_always_false(); }(); diff --git a/tests/test_type_caster_odr_guard_2.cpp b/tests/test_type_caster_odr_guard_2.cpp index 99afba59f..b9400cc5d 100644 --- a/tests/test_type_caster_odr_guard_2.cpp +++ b/tests/test_type_caster_odr_guard_2.cpp @@ -10,7 +10,9 @@ struct type_mrc { struct minimal_real_caster { static constexpr auto name = py::detail::const_name(); +#ifdef PYBIND11_TYPE_CASTER_ODR_GUARD_STRICT PYBIND11_TYPE_CASTER_SOURCE_FILE_LINE +#endif static py::handle cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) {