Date: Thu, 10 Oct 2013 20:44:05 -0500
Nevin Liber <nevin_at_[hidden]> writes:
[...]
| | If someone writes:
| |
| | struct Foo
| | {
| | X x;
| | Y y;
| | Z z;
| |
| | friend bool operator<(Foo const& l, Foo const& r)
| | { return std::tie(l.x, l.y, l.z) < std::tie(r.x, r.y, r.z); }
|
| in my book, that someone writing this isn't a novice, but we have
| already disagreed on this.
|
|
|
| It doesn't take an expert to want to use an object as a key in a set or a map.
I think I can agree with that. But, I don't see how you get from here
to there.
| I was assuming that novices had access to Boost; if you don't want to make
| that assumption, I'm fine with that too.
You're right; I wouldn't make that assumption for a novice.
| | };
| |
| | If X, Y or Z are pointers, this code is broken!
|
| If X, Y, and Z are pointers, I don't think he or she would have written
| that.
|
|
| Show us how you believe a novice would write this:
|
| If X, Y, Z are types that can be used as keys in a set (why should it matter
| whether or not they are pointers?), what does the novice have to write
| so that a struct of them can be used as a key in a set?
The standard documentation says that std::set needs an ordering in order
to deliver some algorithmic complexity. It also says that that
comparator defaults to std::less<T>. To a novice, I will say, if need
to use a type T as a key in a std::map or a std::set, and there is no
standard definition for std::less<T>, then you need to provide one. And
you should std::less<> recursively on the components -- in case you need
to order them too.
-- Gaby
[...]
| | If someone writes:
| |
| | struct Foo
| | {
| | X x;
| | Y y;
| | Z z;
| |
| | friend bool operator<(Foo const& l, Foo const& r)
| | { return std::tie(l.x, l.y, l.z) < std::tie(r.x, r.y, r.z); }
|
| in my book, that someone writing this isn't a novice, but we have
| already disagreed on this.
|
|
|
| It doesn't take an expert to want to use an object as a key in a set or a map.
I think I can agree with that. But, I don't see how you get from here
to there.
| I was assuming that novices had access to Boost; if you don't want to make
| that assumption, I'm fine with that too.
You're right; I wouldn't make that assumption for a novice.
| | };
| |
| | If X, Y or Z are pointers, this code is broken!
|
| If X, Y, and Z are pointers, I don't think he or she would have written
| that.
|
|
| Show us how you believe a novice would write this:
|
| If X, Y, Z are types that can be used as keys in a set (why should it matter
| whether or not they are pointers?), what does the novice have to write
| so that a struct of them can be used as a key in a set?
The standard documentation says that std::set needs an ordering in order
to deliver some algorithmic complexity. It also says that that
comparator defaults to std::less<T>. To a novice, I will say, if need
to use a type T as a key in a std::map or a std::set, and there is no
standard definition for std::less<T>, then you need to provide one. And
you should std::less<> recursively on the components -- in case you need
to order them too.
-- Gaby
Received on 2013-10-11 03:44:21