Skip to content

Commit 53012d2

Browse files
cppver fix
1 parent 121bfcf commit 53012d2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ using MetricAttributes = opentelemetry::sdk::metrics::FilteredOrderedAttributeMa
2828
*/
2929
struct StringViewHash
3030
{
31+
#if __cplusplus >= 202002L
32+
// enable heterogenous lookup in C++20+
3133
using is_transparent = void;
34+
#endif
3235

3336
std::size_t operator()(const std::string &s) const noexcept
3437
{
@@ -37,14 +40,21 @@ struct StringViewHash
3740

3841
std::size_t operator()(opentelemetry::nostd::string_view sv) const noexcept
3942
{
40-
// hash the data without assuming null-termination
43+
#if __cplusplus >= 202002L
44+
return std::hash<opentelemetry::nostd::string_view>{}(
45+
opentelemetry::nostd::string_view{sv.data(), sv.size()});
46+
#else
47+
// pre-C++20 fallback: materialize to std::string
4148
return std::hash<std::string>{}(std::string{sv.data(), sv.size()});
49+
#endif
4250
}
4351
};
4452

4553
struct StringViewEqual
4654
{
55+
#if __cplusplus >= 202002L
4756
using is_transparent = void;
57+
#endif
4858

4959
bool operator()(const std::string &lhs, const std::string &rhs) const noexcept
5060
{
@@ -70,26 +80,24 @@ struct StringViewEqual
7080

7181
/**
7282
* Cross-platform heterogeneous lookup wrapper.
73-
* Falls back to std::string construction on libc++ (macOS),
83+
* Falls back to std::string construction on libc++ (macOS) and pre-c++20,
7484
* but uses direct lookup on libstdc++ (Linux).
7585
*/
7686
inline auto find_hetero(
7787
const std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> &map,
7888
opentelemetry::nostd::string_view key)
7989
{
80-
#if defined(_LIBCPP_VERSION)
81-
// libc++ (macOS) does not yet support heterogeneous lookup
90+
#if defined(_LIBCPP_VERSION) || __cplusplus < 202002L
8291
return map.find(std::string(key));
8392
#else
84-
// libstdc++ supports heterogeneous lookup directly
8593
return map.find(key);
8694
#endif
8795
}
8896

8997
inline auto find_hetero(std::unordered_map<std::string, bool, StringViewHash, StringViewEqual> &map,
9098
opentelemetry::nostd::string_view key)
9199
{
92-
#if defined(_LIBCPP_VERSION)
100+
#if defined(_LIBCPP_VERSION) || __cplusplus < 202002L
93101
return map.find(std::string(key));
94102
#else
95103
return map.find(key);

0 commit comments

Comments
 (0)