Modify

Ticket #2484 (closed Patches: fixed)

Opened 5 years ago

Last modified 4 years ago

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

Reported by: mlcreech@… Owned by: matias
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

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

Change History

Changed 5 years ago by mlcreech@…

Changed 5 years ago by Gonzalo Matamala <gonmator@…>

comment:1 Changed 5 years ago by Gonzalo Matamala <gonmator@…>

  • Version changed from Boost 1.36.0 to Boost 1.39.0

comment:2 Changed 5 years ago by Gonzalo Matamala <gonmator@…>

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 Changed 4 years ago by matias

  • Status changed from new to assigned

comment:4 Changed 4 years ago by matias

  • Status changed from assigned to closed
  • Resolution set to fixed
View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.