Opened 9 years ago

Closed 4 years ago

Last modified 4 years ago

#9223 closed Feature Requests (fixed)

Alllow find(key) with key's type different from the contained value type.

Reported by: mjklaim@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: container
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

In a test, I have this:


struct Id
{
	int value;
};

bool operator==( const Id& a, const Id& b )
{
	return a.value == b.value;
}

bool operator< ( const Id& a, const Id& b )
{
	return a.value < b.value;
}


struct Event
{
	Id id;
	std::string data;
};

bool operator==( const Entity& a, const Entity& b )
{
	return a.id == b.id;
}

bool operator< ( const Entity& a, const Entity& b )
{
	return a.id < b.id;
}

bool operator==( const Id& a, const Entity& b )
{
	return a == b.id;
}
bool operator==( const Entity& a, const Id& b )
{
	return b == a;
}

Here value type Event can be compared to Id. Now this test can't work:

boost::container::flat_set<Event> events;
events.find( Id{42} ); // ERROR: Id isn't an Event!

I would like this to compile because it would allow me to have a set of objects containing a state which can be overridden. In this test Event's value is it's id, but it contain associated data too which are not part of the comparison and can change when we insert new Events with the same id but different data.

This feature looks like a recent c++ proposal: n3573

Attachments (2)

anyvalueset.hpp (4.9 KB ) - added by mjklaim@… 9 years ago.
Example of real-world code that would be simplified by the feature.
anyvalueset.cpp (4.1 KB ) - added by mjklaim@… 9 years ago.
Test of the real world example.

Download all attachments as: .zip

Change History (6)

by mjklaim@…, 9 years ago

Attachment: anyvalueset.hpp added

Example of real-world code that would be simplified by the feature.

by mjklaim@…, 9 years ago

Attachment: anyvalueset.cpp added

Test of the real world example.

comment:1 by mjklaim@…, 9 years ago

As you will see in the example attached (tests uses Google Test) the call to find allow different types of keys than the underlying value type, as expressed in the "unique_key_values" test.

Unfortunately I had to write a custom "find" function in the AnyValueSet::SetOf class, so that the key is used to compare directly with the value, both types being preserved.

Fortunately, flat_set being basically a vector, it's not really a problem on performance to write the search this way.

Anyway, I wanted to give my use case. The find function could be simplified by just providing the key to flat_set instead of having to do the search manually.

comment:2 by Ion Gaztañaga, 9 years ago

C++14 will include new find functions for associative containers:

template <class K> iterator find(const K& x); template <class K> const_iterator find(const K& x) const;

"returns an iterator pointing to an element with key r such that !c(r, ke) && !c(ke, r), or a_tran.end() if such an element is not found"

The original proposal was: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3657.htm

In the future, Boost.Container will try to implement C++14 features so I'll left this issue open until this feature is implemented.

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

Resolution: fixed
Status: newclosed

Better late than never. Commit:

https://github.com/boostorg/container/commit/48c21e3187cd95b42803efe297842b52c896090e

Many thanks for the report.

comment:4 by mjklaim@…, 4 years ago

Thanks for your work!

Note: See TracTickets for help on using tickets.