6

I'm trying to dissect this websocket++ example https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cpp

At line 126, there is typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;. I'm wondering why it isn't merely typedef std::set<connection_hdl> con_list;.

I've read How does the Comma Operator work and http://en.cppreference.com/w/cpp/memory/owner_less but I'm not sure what the purpose is.

What is actually stored in the std::set? What does std::owner_less actually do? What is the purpose of making a container with this structure instead of storing the connection_hdls themselves?

Community
  • 1
  • 1
  • 2
    This has nothing to do with the comma operator, and the page you linked to for `std::owner_less` should answer your question directly. – ildjarn Mar 29 '13 at 21:41
  • 1
    `std::set` takes multiple template parameters, but only the first is explicitly required; the rest have default types if you don't define them. The second parameter is a binary function used for sorting. – Collin Dauphinee Mar 29 '13 at 21:43
  • 2
    BTW, `connection_hdl` is a `std::weak_ptr`. – Jesse Good Mar 29 '13 at 21:52
  • 1
    @Joe : If you read the page you linked to, you would know that `std::weak_ptr<>` has no `operator<`, so it simply wouldn't compile without a special comparitor. ;-] – ildjarn Mar 29 '13 at 22:22
  • @ildjarn ohhhhhhhhhhh, ok. I'm new to c++, so it took a while to sink in lol. you made it much clearer. ty! –  Mar 29 '13 at 22:25

1 Answers1

8

This has nothing to do with comma operator. A set<T> requires that T be less-than-comparable, i.e. operator < should be defined for T. The reason for this is that the set keeps its elements in an ordered fashion(via one kind of tree or another). You can supply a set with a second template argument - a functor that compares two elements so that the set can use that comparison. std::owner_less is a predicate template that operates on shared and weak pointers and performs owner-based (as opposed to value-based) ordering.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434