octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: auto and const_iterators


From: John W. Eaton
Subject: Re: auto and const_iterators
Date: Wed, 2 May 2018 17:52:50 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 05/02/2018 05:29 PM, Rik wrote:

I noticed that you changed most instances of for loops to use the keyword
auto to determine type.  This definitely helps to reduce line length and
improve the readability of the code, but I don't think auto can be used to
get a const_iterator.  Taking one example from lo-regexp.cc

-        regexp::match_data::const_iterator p = rx_lst.begin ();
+        auto p = rx_lst.begin ();

The method begin() is going to return a regular iterator.  This will
certainly work, but then there won't be a compile time check if the code
attempts to modify rx_lst accidentally, which it shouldn't be doing.  I
don't think you can solve it by using 'const auto p', because then the
iterator itself is fixed so p++ will fail if you try to advance.  If this
were C++14 then we could call the method cbegin() which has a function
prototype that returns const_iterator so auto would correctly deduce the
right type.  Stack Overflow doesn't recommend using auto in this scenario:
https://stackoverflow.com/questions/15233188/how-do-i-get-a-const-iterator-using-auto#15233309.
Although, if you're open to further code changes then it would be possible
to work on a const reference to the original.

Oops.

const auto& c_rx_list = rx_lst;
auto p = c_rx_list.begin ();

Most of the iterators in cset 3ff9192b676e are not constant so it's really
deciding what to do about the handful that are const.  Should they just be
reverted to using long syntax?

Either solution is OK with me, but I see more lines that remove const_iterator than iterator. Are the only ones we have to change the ones where the object itself is not const but we were accessing elements with a const_iterator?

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]