Why isn't constant range used for const_iterator usage?

If I only want to expose a constant iterator with an object:

class MyList
{
  public:
    const_iterator begin() const;
    const_iterator end() const;
  private:
    iterator begin();
    iterator end();
};

      

It seems that I have to use the range constant version based on:

MyList list;
...
for(const auto & value : list)
{
}

      

The compiler complains that begin

they end

are private. Why isn't he using versions const_iterator

?

+3


source to share


1 answer


Overload resolution is done prior to access checking to avoid the magic code break by simply changing the access specifiers.

What happens to the expression afterwards (its type) is not taken into account for this. If necessary, the compiler will try to find the correct and unambiguous sequence of transformations later.



So, select begin

and end

for object not const

, and then the compiler stumbles upon that big private

-sign.

+2


source







All Articles