...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <functional> #include <iostream> #include <set> void f() { std::set<int, std::less_equal<int>> Ss{5, 10, 20}; for (auto Rr = Ss.equal_range(10); Rr.first != Rr.second; ++Rr.first) { std::cout << *Rr.first << std::endl; } } |
Compliant Solution
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream> #include <set> void f() { std::set<int> Ss{5, 10, 20}; for (auto Rr = Ss.equal_range(10); Rr.first != Rr.second; ++Rr.first) { std::cout << *Rr.first << std::endl; } } |
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream> #include <set> class S { int Ii, Jj; public: S(int Ii, int Jj) : Ii(Ii), Jj(Jj) {} friend bool operator<(const S &LHSlhs, const S &RHSrhs) { return LHSlhs.Ii < RHSrhs.Ii && LHSlhs.Jj < RHSrhs.Jj; } friend std::ostream &operator<<(std::ostream &OSos, const S& Oo) { OSos << "Ii: " << Oo.Ii << ", Jj: " << Oo.Jj; return OSos; } }; void f() { std::set<S> Tt{S(1, 1), S(1, 2), S(2, 1)}; for (auto Vv : Tt) { std::cout << Vv << std::endl; } } |
Compliant Solution
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <iostream> #include <set> #include <tuple> class S { int Ii, Jj; public: S(int Ii, int Jj) : Ii(Ii), Jj(Jj) {} friend bool operator<(const S &LHSlhs, const S &RHSrhs) { return std::tie(LHSlhs.Ii, LHSlhs.Jj) < std::tie(RHSrhs.Ii, RHSrhs.Jj); } friend std::ostream &operator<<(std::ostream &OSos, const S& Oo) { OSos << "Ii: " << Oo.Ii << ", Jj: " << Oo.Jj; return OSos; } }; void f() { std::set<S> Tt{S(1, 1), S(1, 2), S(2, 1)}; for (auto Vv : Tt) { std::cout << Vv << std::endl; } } |
Risk Assessment
...