Skip to content

Commit ad67881

Browse files
committed
Make more similar to current pybind/pybind11#4022, but with specs_odr_guard() constructor accessing translation_unit_local.value
1 parent f6549ec commit ad67881

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

caster_odr_specs_odr_guard/car.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace vehicles {
55
struct vehicle {};
66

77
struct car_specs {
8-
static constexpr unsigned unique_id = 10;
8+
static constexpr engine::tu_local_unsigned unique_id = 10;
99
int power() const { return 100; }
1010
};
1111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set -e
22
set -x
3-
clang++ -std=c++11 car.cc truck.cc vehicle_specs.cc && ./a.out
4-
clang++ -std=c++11 truck.cc car.cc vehicle_specs.cc && ./a.out
3+
clang++ -std=c++17 car.cc truck.cc vehicle_specs.cc && ./a.out
4+
clang++ -std=c++17 truck.cc car.cc vehicle_specs.cc && ./a.out
55
rm a.out

caster_odr_specs_odr_guard/engine.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,34 @@
66

77
namespace engine {
88

9+
namespace {
10+
struct tu_local_unsigned {
11+
unsigned value;
12+
constexpr tu_local_unsigned(unsigned v) : value(v) {}
13+
};
14+
} // namespace
15+
916
template <typename T>
1017
struct specs {
11-
static constexpr unsigned unique_id = 0;
18+
static constexpr tu_local_unsigned unique_id = 0;
1219
int power() const { return 0; }
1320
};
1421

1522
namespace {
1623

1724
template <typename T>
1825
struct specs_odr_guard : specs<T> {
19-
static int translation_unit_local;
26+
static volatile tu_local_unsigned translation_unit_local;
27+
specs_odr_guard() {
28+
if (translation_unit_local.value) {
29+
}
30+
}
2031
};
2132

2233
template <typename T>
23-
int specs_odr_guard<T>::translation_unit_local = []() {
34+
volatile tu_local_unsigned specs_odr_guard<T>::translation_unit_local = []() {
2435
fprintf(stdout, "MAKE_SPECS_ODR_GUARD %s %u\n", typeid(T).name(),
25-
specs<T>::unique_id);
36+
specs<T>::unique_id.value);
2637
fflush(stdout);
2738
return 0;
2839
}();
@@ -34,8 +45,6 @@ using make_specs = specs_odr_guard<T>;
3445

3546
template <typename T>
3647
int power() {
37-
if (make_specs<T>::translation_unit_local) {
38-
}
3948
return make_specs<T>().power();
4049
}
4150

caster_odr_specs_odr_guard/truck.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace vehicles {
55
struct vehicle {};
66

77
struct truck_specs {
8-
static constexpr unsigned unique_id = 30;
8+
static constexpr engine::tu_local_unsigned unique_id = 30;
99
int power() const { return 300; }
1010
};
1111

0 commit comments

Comments
 (0)