[Top][All Lists]

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

auto and const_iterators

From: Rik
Subject: auto and const_iterators
Date: Wed, 2 May 2018 14:29:19 -0700


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:
Although, if you're open to further code changes then it would be possible
to work on a const reference to the original.

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?


reply via email to

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