@@ -28,7 +28,10 @@ using MetricAttributes = opentelemetry::sdk::metrics::FilteredOrderedAttributeMa
28
28
*/
29
29
struct StringViewHash
30
30
{
31
+ #if __cplusplus >= 202002L
32
+ // enable heterogenous lookup in C++20+
31
33
using is_transparent = void ;
34
+ #endif
32
35
33
36
std::size_t operator ()(const std::string &s) const noexcept
34
37
{
@@ -37,14 +40,21 @@ struct StringViewHash
37
40
38
41
std::size_t operator ()(opentelemetry::nostd::string_view sv) const noexcept
39
42
{
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
41
48
return std::hash<std::string>{}(std::string{sv.data (), sv.size ()});
49
+ #endif
42
50
}
43
51
};
44
52
45
53
struct StringViewEqual
46
54
{
55
+ #if __cplusplus >= 202002L
47
56
using is_transparent = void ;
57
+ #endif
48
58
49
59
bool operator ()(const std::string &lhs, const std::string &rhs) const noexcept
50
60
{
@@ -70,26 +80,24 @@ struct StringViewEqual
70
80
71
81
/* *
72
82
* 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 ,
74
84
* but uses direct lookup on libstdc++ (Linux).
75
85
*/
76
86
inline auto find_hetero (
77
87
const std::unordered_map<std::string, bool , StringViewHash, StringViewEqual> &map,
78
88
opentelemetry::nostd::string_view key)
79
89
{
80
- #if defined(_LIBCPP_VERSION)
81
- // libc++ (macOS) does not yet support heterogeneous lookup
90
+ #if defined(_LIBCPP_VERSION) || __cplusplus < 202002L
82
91
return map.find (std::string (key));
83
92
#else
84
- // libstdc++ supports heterogeneous lookup directly
85
93
return map.find (key);
86
94
#endif
87
95
}
88
96
89
97
inline auto find_hetero (std::unordered_map<std::string, bool , StringViewHash, StringViewEqual> &map,
90
98
opentelemetry::nostd::string_view key)
91
99
{
92
- #if defined(_LIBCPP_VERSION)
100
+ #if defined(_LIBCPP_VERSION) || __cplusplus < 202002L
93
101
return map.find (std::string (key));
94
102
#else
95
103
return map.find (key);
0 commit comments