Assigning a const_iterator to an iterator

Varun Hiremath

I have the below snippet of code (which you can run here: http://coliru.stacked-crooked.com/a/2f62134b5c125051)

#include <iostream>
#include <set>
#include <map>

int main() 
{
    std::set<std::pair<const int, const int>> const mySet{{0,0}}; // value_type = std::pair<const int, const int>
    for (std::set<std::pair<const int, const int>>::iterator it  = mySet.cbegin(); it != mySet.cend(); ++it)
    {
        std::cout<<"set it = " << it->first << " " << it->second << std::endl;
    }

    std::map<const int, const int> const myMap{{0,0}}; // value_type = std::pair<const int, const int>
    for (std::map<const int, const int>::iterator it  = myMap.cbegin(); it != myMap.cend(); ++it)
    {
        std::cout<<"map it = " << it->first << " " << it->second << std::endl;
    }   
}

Can someone please explain me why for std::set the below does not throw any error:

std::set<std::pair<const int, const int>>::iterator it  = mySet.cbegin();

while for std::map the below throws error (no known conversion from _Rb_tree_const_iterator<std::pair<const int, const int> > to _Rb_tree_iterator<std::pair<const int, const int> >) as expected:

std::map<const int, const int>::iterator it  = myMap.cbegin();

How does it work for std::set? Shouldn't assigning a const_iterator to an iterator always throw an error?

largest_prime_is_463035818

You cannot modify a sets elements. Keys must be const to ensure the invariant promised by the set: elements are sorted and unique. A maps elements are sorted too, but you can modify the mapped value of the elements (keys are const as well). Elements of a std::map<A,B> are std::pair<const A,B>.

On cppreference you can read that member aliases for iterators of std::set are

iterator Constant LegacyBidirectionalIterator

const_iterator Constant LegacyBidirectionalIterator

They are both const iterators.

On the other hand for a std::map they are:

iterator LegacyBidirectionalIterator

const_iterator Constant LegacyBidirectionalIterator

Assigning a const_iterator to a non-const one is wrong. Its like trying to cast const away by assigning a pointer to const to a pointer to non-const. It won't work because you cannot make something that cannot be modified modifiable. That would break const-correctness.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterator VS const_iterator, using it with distance()

what is the difference between const_iterator and iterator?

Boost const_iterator to iterator conversion

Is there a way to convert local_iterator to const_iterator or iterator?

Assigning to an element via iterator

map::const_iterator mapped type is not const

erase gets const_iterator but is called with iterator (non-const)

find_if not working with const_iterator

Visual Studio const_iterator Assignment Error

Trouble implementing const_iterator in c++

understanding the type of dereference - const_iterator

Is vector<int>::const_iterator an output_iterator?

`iterator` and `const_iterator` are not required members of STL containers?

Is comparison of const_iterator with iterator well-defined?

no viable conversion from returned value of type const_iterator to iterator

Why does Scott Meyers recommend to prefer `iterator` to `const_iterator`

I get const_iterator instead of iterator from std::set

Iterator and const_iterator operator++ post and prefix

Iterator method fails to inherit from Const_iterator

How to remove code duplication const_iterator and iterator in my case?

property tree put/erase with const iterator, or how to convert const_iterator to iterator

Why doesn't const range based for use const_iterator?

How to get const_iterator when the T is 'const std::map',?

Algorithm not working on const_iterator for non-const object

How do I convert iterator to const_iterator in my custom list iterator class?

Why does "std::begin()" always return "const_iterator" in such a case?

Why were the std::vector::erase parameters changed to const_iterator?

c++ sorted view of range - how to create const_iterator?

Problem with const_iterator being called outside class