Opened 7 years ago

Closed 6 years ago

Last modified 6 years 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:

Description

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;
v.insert(...);

// 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.

Change History (4)

comment:1 by Ion Gaztañaga, 7 years ago

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

comment:2 by martin.seemann@…, 7 years ago

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

comment:3 by Ion Gaztañaga, 6 years ago

Resolution: fixed
Status: newclosed

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

https://github.com/boostorg/intrusive/commit/4014562502d7a9fb9a13b09a908e0d757e368969

comment:4 by fdegros@…, 6 years ago

Excellent. Thanks for fixing it.

Note: See TracTickets for help on using tickets.