Jump to content
Fraser

std::cbegin can return an iterator

Recommended Posts

I am using C++ Builder 10.4 with Dinkumware 8.03.  The non-member function std::cbegin sometimes returns an iterator when it should always be a const_iterator.  The following program will not compile due to b1 being deduced as iterator and b2 a const_iterator.  There is no problem with c1 and c2.  The fix is to add const to the parameter _Container & as described in the book.

 

 

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> a1(100, 2);
    std::vector<int> const a2(100, 3);
    auto const
        b1= std:: cbegin(a1),
        b2= std::cbegin(a2);
    /*auto const
        c1= a1.cbegin(),
        c2= a2.cbegin();  */
   // See item 13 of Effective Modern C++ for explanation.
    return 0;
}

 /* line 1672 of xiter
template<class _Container>
_CONST_FUN auto inline cbegin(_Container& _Cont) _NOEXCEPT_OP(_NOEXCEPT_OP(_STD begin(_Cont)))
        -> decltype(_STD begin(_Cont))
    {    // get beginning of sequence
    return (_STD begin(_Cont));
    }     */

Share this post


Link to post
7 hours ago, Fraser said:

The non-member function std::cbegin sometimes returns an iterator when it should always be a const_iterator.  The following program will not compile due to b1 being deduced as iterator and b2 a const_iterator.

Then that is a bug that needs to be reported to Embarcadero/Dinkumware, because std::cbegin() is defined as always returning a const_iterator when passed a standard container.  The input parameter of std::cbegin() is supposed to be taken as a const, but that is not the case in the example you have shown.

Share this post


Link to post

The same problem occurs with std::cend.  It is easily fixed by inserting const.  I have reported this to Dinkumware.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×