]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/nlohmann-json/doc/mkdocs/docs/api/basic_json/operator_eq.md
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / nlohmann-json / doc / mkdocs / docs / api / basic_json / operator_eq.md
1 # <small>nlohmann::basic_json::</small>operator==
2
3 ```cpp
4 bool operator==(const_reference lhs, const_reference rhs) noexcept;
5
6 template<typename ScalarType>
7 bool operator==(const_reference lhs, const ScalarType rhs) noexcept;
8
9 template<typename ScalarType>
10 bool operator==(ScalarType lhs, const const_reference rhs) noexcept;
11 ```
12
13 Compares two JSON values for equality according to the following rules:
14
15 - Two JSON values are equal if (1) they are not discarded, (2) they are from the same type, and (3) their stored values
16 are the same according to their respective `operator==`.
17 - Integer and floating-point numbers are automatically converted before comparison. Note that two NaN values are always
18 treated as unequal.
19
20 ## Template parameters
21
22 `ScalarType`
23 : a scalar type according to `std::is_scalar<ScalarType>::value`
24
25 ## Parameters
26
27 `lhs` (in)
28 : first value to consider
29
30 `rhs` (in)
31 : second value to consider
32
33 ## Return value
34
35 whether the values `lhs` and `rhs` are equal
36
37 ## Exception safety
38
39 No-throw guarantee: this function never throws exceptions.
40
41 ## Complexity
42
43 Linear.
44
45 ## Notes
46
47 !!! note
48
49 - NaN values never compare equal to themselves or to other NaN values.
50 - JSON `#!cpp null` values are all equal.
51 - Discarded values never compare equal to themselves.
52
53 !!! note
54
55 Floating-point numbers inside JSON values numbers are compared with `json::number_float_t::operator==` which is
56 `double::operator==` by default. To compare floating-point while respecting an epsilon, an alternative
57 [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39)
58 could be used, for instance
59
60 ```cpp
61 template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
62 inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
63 {
64 return std::abs(a - b) <= epsilon;
65 }
66 ```
67
68 Or you can self-defined operator equal function like this:
69
70 ```cpp
71 bool my_equal(const_reference lhs, const_reference rhs)
72 {
73 const auto lhs_type lhs.type();
74 const auto rhs_type rhs.type();
75 if (lhs_type == rhs_type)
76 {
77 switch(lhs_type)
78 // self_defined case
79 case value_t::number_float:
80 return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon();
81 // other cases remain the same with the original
82 ...
83 }
84 ...
85 }
86 ```
87
88 ## Examples
89
90 ??? example
91
92 The example demonstrates comparing several JSON types.
93
94 ```cpp
95 --8<-- "examples/operator__equal.cpp"
96 ```
97
98 Output:
99
100 ```json
101 --8<-- "examples/operator__equal.output"
102 ```
103
104 ??? example
105
106 The example demonstrates comparing several JSON types against the null pointer (JSON `#!json null`).
107
108 ```cpp
109 --8<-- "examples/operator__equal__nullptr_t.cpp"
110 ```
111
112 Output:
113
114 ```json
115 --8<-- "examples/operator__equal__nullptr_t.output"
116 ```
117
118 ## Version history
119
120 - Added in version 1.0.0.