How is
Comp{}
instd::equal_range
triggering the Compare operation? IsComp{}
indicating a R-Value being created?#include <algorithm> #include <vector> #include <iostream> struct S { int number; char name; // note: name is ignored by this comparison operator bool operator< ( const S& s ) const { return number < s.number; } }; int main() { // note: not ordered, only partitioned w.r.t. S defined below const std::vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} }; std::cout << "\n" "Using heterogeneous comparison: "; struct Comp { bool operator() ( const S& s, int i ) const { return s.number < i; } bool operator() ( int i, const S& s ) const { return i < s.number; } }; const auto p2 = std::equal_range(vec.begin(),vec.end(), 2, Comp{}); for ( auto i = p2.first; i != p2.second; ++i ) std::cout << i->name << ' '; }
Asked
Active
Viewed 87 times
2

Test
- 564
- 3
- 12
-
`Comp{}` is a (default-constructed) temporary. – Jarod42 Jan 14 '22 at 09:20
-
@Jarod42 - Could you please elaborate in view of my questions? – Test Jan 14 '22 at 09:40
-
1You have 3 different questions. Not sure what you already understand to create an answer to all questions. (that's why one question by question is better). – Jarod42 Jan 14 '22 at 09:46