c++ - force key type of a std::map not to be const -


c++ references tells std::map

typedef pair<const key, t> value_type; 

is possible force key type not const ? need in template method

template<class t> // t represent map in general (std::map, boost::unordered_map or whatever..) void foo(const t& m) {   typename t::value_type::first_type x;   x=0; // wrong because x const ... } 

no, it's not.

this because map performs internal ordering based on key. if modify key yourself, willy-nilly, hell break loose.

you should use provided api functions; use of 1 results in changing key value (actually don't think do), appropriate internal re-ordering may take place.

think of getters , setters, , use in providing alternative messy/dangerous direct member access.


however, write this:

template<class t> void foo(const t& m) {    typename t::key_type x;    x = 0; } 

std::map type aliases

key_type                key mapped_type             t value_type              pair<const key,t> key_compare             compare value_compare           nested class compare elements allocator_type          allocator reference               allocator::reference const_reference         allocator::const_reference iterator                bidirectional iterator const_iterator          constant bidirectional iterator size_type               unsigned integral type (usually same size_t) difference_type         signed integral type (usually same ptrdiff_t) pointer                 allocator::pointer const_pointer           allocator::const_pointer reverse_iterator        reverse_iterator<iterator> const_reverse_iterator  reverse_iterator<const_iterator> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -