Skip to content

Commit 96f2742

Browse files
committed
use static_const for callable objects
1 parent 670d2ef commit 96f2742

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/viam/sdk/common/private/repeated_ptr_convert.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ struct from_repeated_field_ {
3838
}
3939
};
4040

41-
constexpr to_repeated_field_ to_repeated_field;
42-
constexpr from_repeated_field_ from_repeated_field;
41+
namespace {
42+
43+
constexpr auto& to_repeated_field = proto_convert_details::static_const<to_repeated_field_>::value;
44+
45+
constexpr auto& from_repeated_field =
46+
proto_convert_details::static_const<from_repeated_field_>::value;
47+
48+
} // namespace
4349

4450
} // namespace impl
4551
} // namespace sdk

src/viam/sdk/common/proto_convert.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ namespace sdk {
1111

1212
namespace proto_convert_details {
1313

14+
// This is copied from range-v3 to allow the definition of callable object instances without
15+
// ODR/linkage issues. It is obviated in C++17 and onwards by constexpr inline.
16+
template <typename T>
17+
struct static_const {
18+
static constexpr const T value{};
19+
};
20+
21+
template <typename T>
22+
constexpr const T static_const<T>::value;
23+
1424
// This struct should be explicitly specialized with a
1525
// void operator()(const SdkType&, common::v1::ApiType*) const
1626
// to provide API/ABI insulated proto conversion
@@ -55,15 +65,21 @@ struct from_proto_impl {
5565

5666
namespace v2 {
5767

68+
namespace {
69+
5870
/// @brief Function object implementing conversion from an SDK type to an API type.
5971
/// This callable works for any type with a proto_convert_details::to_proto specialization as
6072
/// described above.
61-
constexpr proto_convert_details::to_proto_impl to_proto{};
73+
constexpr auto& to_proto =
74+
proto_convert_details::static_const<proto_convert_details::to_proto_impl>::value;
6275

6376
/// @brief Function object implementing conversion from an API type to an SDK type.
6477
/// This callable works for any type with a proto_convert_details::from_proto specialization as
6578
/// described above.
66-
constexpr proto_convert_details::from_proto_impl from_proto{};
79+
constexpr auto& from_proto =
80+
proto_convert_details::static_const<proto_convert_details::from_proto_impl>::value;
81+
82+
} // namespace
6783

6884
} // namespace v2
6985

src/viam/sdk/spatialmath/geometry.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <tuple>
66

77
#include <viam/sdk/common/pose.hpp>
8-
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
98
#include <viam/sdk/common/proto_convert.hpp>
109
#include <viam/sdk/spatialmath/orientation.hpp>
1110

@@ -22,6 +21,7 @@ class GetGeometriesResponse;
2221

2322
class GeoPoint;
2423
class GeoGeometry;
24+
2525
} // namespace v1
2626
} // namespace common
2727
} // namespace viam

0 commit comments

Comments
 (0)