vendredi 3 mars 2017

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

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> coll;

    decltype(std::begin(std::declval<vector<int>>()))
        pos_1 = coll.begin();
    auto pos_2 = coll.begin();

    cout << typeid(decltype(pos_1)).name() << endl;
    cout << typeid(decltype(pos_2)).name() << endl;
}

My compiler is clang 4.0. The output is:

class std::_Vector_const_iterator<class std::_Vector_val<struct std::_Simple_types<int> > >
class std::_Vector_iterator<class std::_Vector_val<struct std::_Simple_types<int> > >

That means: pos_1 = pos_2; is ok, while pos_2 = pos_1; is not ok.

Why does std::begin() always return const_iterator rather than iterator in such a case?

Aucun commentaire:

Enregistrer un commentaire