@@ -92,49 +92,52 @@ inline void type_caster_odr_guard_impl(const std::type_info &intrinsic_type_info
92
92
}
93
93
}
94
94
95
+ struct tu_local_no_data_always_false_base {
96
+ explicit operator bool () const noexcept { return false ; }
97
+ };
98
+
95
99
namespace { // WARNING: This creates an ODR violation in the ODR guard itself,
96
100
// but we do not have anything better at the moment.
97
101
// The ODR violation here does not involve any data at all.
98
102
// See also: Comment near top of descr.h & WARNING in descr.h
99
103
100
- struct tu_local_no_data_always_false {
101
- explicit operator bool () const noexcept { return false ; }
104
+ struct tu_local_no_data_always_false : tu_local_no_data_always_false_base {
105
+ explicit tu_local_no_data_always_false (const std::type_info &intrinsic_type_info,
106
+ const src_loc &sloc,
107
+ bool throw_disabled) {
108
+ type_caster_odr_guard_impl (intrinsic_type_info, sloc, throw_disabled);
109
+ }
102
110
};
103
111
104
112
} // namespace
105
113
106
114
template <typename IntrinsicType, typename TypeCasterType>
107
115
struct type_caster_odr_guard : TypeCasterType {
108
- static tu_local_no_data_always_false translation_unit_local;
116
+ static tu_local_no_data_always_false_base * translation_unit_local;
109
117
110
118
type_caster_odr_guard () {
111
119
// Possibly, good optimizers will elide this `if` (and below) completely.
112
120
// It is needed only to trigger the TU-local mechanisms.
113
- if (translation_unit_local) {
121
+ if (* translation_unit_local) {
114
122
}
115
123
}
116
124
117
125
// The original author of this function is @amauryfa
118
126
template <typename CType, typename ... Arg>
119
127
static handle cast (CType &&src, return_value_policy policy, handle parent, Arg &&...arg) {
120
- if (translation_unit_local) {
128
+ if (* translation_unit_local) {
121
129
}
122
130
return TypeCasterType::cast (
123
131
std::forward<CType>(src), policy, parent, std::forward<Arg>(arg)...);
124
132
}
125
133
};
126
134
127
135
template <typename IntrinsicType, typename TypeCasterType>
128
- tu_local_no_data_always_false
129
- type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local
130
- = []() {
131
- // Executed only once per process (e.g. when a PYBIND11_MODULE is initialized).
132
- // Conclusively tested vi test_type_caster_odr_guard_1, test_type_caster_odr_guard_2:
133
- // those tests will fail if the sloc here is not working as intended (TU-local).
134
- type_caster_odr_guard_impl (typeid (IntrinsicType),
136
+ tu_local_no_data_always_false_base*
137
+ type_caster_odr_guard<IntrinsicType, TypeCasterType>::translation_unit_local = []() {
138
+ return new tu_local_no_data_always_false (typeid (IntrinsicType),
135
139
TypeCasterType::name.sloc ,
136
140
PYBIND11_DETAIL_TYPE_CASTER_ODR_GUARD_THROW_DISABLED);
137
- return tu_local_no_data_always_false ();
138
141
}();
139
142
140
143
PYBIND11_NAMESPACE_END (detail)
0 commit comments