Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top