auto it = vector.begin() resulting type is not convertible to const_iterator

后端 未结 3 952
予麋鹿
予麋鹿 2021-01-13 02:41

Containers are required to provide an iterator type which is implicitly convertible to a const_iterator. Given this, I am trying to use auto

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 02:53

    Why does auto deduce a typ[e in this case that is not convertible to const_iterator?

    • You have two available overloads for begin() :

    Signatures:

    iterator begin();
    
    const_iterator begin() const;
    

    You declared your vector as Packets test (100);, which is non const.

    If you declare it const, auto type deduction will have the second begin() overload as a best (and unique) match.

    It compiles and run with this simple fix.

提交回复
热议问题