Opened 20 months ago

Closed 15 months ago

Last modified 15 months ago

#11994 closed Bugs (fixed)

Support intrusive container key extractors that return the key by value

Reported by: fdegros@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: intrusive
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc:


Map and multimap-like interface for associative containers specifies that the "key extractor" passed by key_of_value must provide a member function to obtain a const reference to the key stored inside a value_type.

According to my experience, if the key extractor returns the key by value instead of const reference, then the code compiles but the program crashes a run time. This is a real pitfall.

Consider supporting key extraction by value as well. It would allow to use keys that are not stored directly in the value_type, or not directly accessible by reference.

For example:

using boost::intrusive;

struct my_node: set_base_hook<> {
  string s;

// The key is the length of my_node::s.
struct by_length {
  using key_type = std::size_t;

  // Return key by value.
  std::size_t operator()(const my_node& n) const {
    return n.s.length();

multiset<std::string, key_of_value<by_length>> v;

// Find a string by length.
const auto it = v.find(3);

If this is not possible, then add a static assertion to make sure that the code doesn't compile at all.

Attachments (0)

Change History (4)

comment:1 Changed 19 months ago by Ion Gaztañaga

Returning keys by value sounds interesting, I'm not sure about the impact, I'll investigate it. Thanks for the proposal.

comment:2 Changed 18 months ago by martin.seemann@…

Btw.: The functor needs a 'type' member instead of 'key_type'. It would be nice to fix this in the documentation.

comment:3 Changed 15 months ago by Ion Gaztañaga

Resolution: fixed
Status: newclosed

Thanks for the report and patience. Fixed in develop, commit:

comment:4 Changed 15 months ago by fdegros@…

Excellent. Thanks for fixing it.

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Ion Gaztañaga.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.