Opened 14 years ago

Closed 13 years ago

#2484 closed Patches (fixed)

Const-overload needed for bimap associative_container_adaptor::count()

Reported by: mlcreech@… Owned by: Matias Capeletto
Milestone: Boost 1.38.0 Component: bimap
Version: Boost 1.39.0 Severity: Problem
Keywords: Cc:

Description

Would it be possible to include a const-overloaded version of count() for associative containers in boost::bimaps? I had some code like this:

void CMyClass::func(size_t key) const {
       size_t num_elements=_member_map.count(key);
       ...
}

where _member_map was a std::map, and it worked fine. When I changed _member_map to a bimap, the equivalent code:

void CMyClass::func(size_t key) const {
       size_t num_elements=_member_map.left.count(key);
       ...
}

gives an error, because count() is non-const (whereas GCC's std::map::count() is const). That's presumably done because the STL documentation declares count() as non-const (not sure why), but it doesn't seem there would be any harm in adding a const-overload so that this kind of code would compile when the underlying type supports it. The attached patch fixes the problem for me.

Thanks

Attachments (2)

07-const-count-overload.patch (883 bytes ) - added by mlcreech@… 14 years ago.
associative_container_adaptor-count-const-fix.patch (543 bytes ) - added by Gonzalo Matamala <gonmator@…> 13 years ago.

Download all attachments as: .zip

Change History (6)

by mlcreech@…, 14 years ago

by Gonzalo Matamala <gonmator@…>, 13 years ago

comment:1 by Gonzalo Matamala <gonmator@…>, 13 years ago

Version: Boost 1.36.0Boost 1.39.0

comment:2 by Gonzalo Matamala <gonmator@…>, 13 years ago

I think overloading count() is a wrong fix. The right fix is just adding const qualifier.

The C++ standard declare const() member of map, multimap, set and multi set as const [23.3.1, 23.3.2, 23.3.3, 23.3.4]:

size_type count(const key_type& x) const;

Also, the bimap documentation (http://www.boost.org/doc/libs/1_40_0/libs/bimap/doc/html/boost_bimap/reference/set_of_reference.html#boost_bimap.reference.set_of_reference.__multi__set_of_views.set_operations) declare count() member of

[multi]set_of as const:
template< class CompatibleKey >
size_type count(const key_type & x) const;

comment:3 by Matias Capeletto, 13 years ago

Status: newassigned

comment:4 by Matias Capeletto, 13 years ago

Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.