lmi
[Top][All Lists]
Advanced

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

Re: [lmi] [PATCH] C++ m11n: range-based for loops


From: Greg Chicares
Subject: Re: [lmi] [PATCH] C++ m11n: range-based for loops
Date: Sat, 14 Jan 2017 16:14:55 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.5.1

On 2017-01-14 00:11, Greg Chicares wrote:
[...]
> I've prepared the document pasted inline below to guide me through the
> remaining work. First, I'll dispatch the "common" files that were
> changed by both 52a and 52b
[...]
>  ------clang-tidy------               ------VZ-manual------
>                       -----common------
> 
>  authenticity_test.cpp        |  7 ||*authenticity_test.cpp        |  6
>  census_view.cpp              | 56 || census_view.cpp              | 18
>  crc32.cpp                    |  4 || crc32.cpp                    |  4
>  dbdict.cpp                   | 15 ||*dbdict.cpp                   | 10
>  group_values.cpp             | 25 || group_values.cpp             | 73
>  input_sequence.cpp           | 16 ||*input_sequence.cpp           |  2
>  input_sequence_entry.cpp     | 20 || input_sequence_entry.cpp     |  6
>  ledger.cpp                   | 18 || ledger.cpp                   | 20
>  wx_table_generator.cpp       | 18 ||*wx_table_generator.cpp       |  8

Zeroth, I'll reduce the number of "common" files. (More asterisks added
above, as explained below.)

* input_sequence.cpp

Here, the only difference is:

    for(double vi : v)    <-- 52a
    for(double vi: v)     <-- 52b

One consistent style must be chosen, and I'll choose to write spaces
both before and after the colon. This is a common practice, contrasting
with the lack of space preceding semicolons in old-style for loops:

http://www.stroustrup.com/C++11FAQ.html
for (int i=0; i<b.size(); ++i)
for (auto& x : v) ++x;
for (const auto x : { 1,2,3,5,8,13,21,34 }) cout << x << '\n';

and an immediately-visible distinction between the two styles may aid
readability. In the old style, there's no space before the semicolon
because it wants to bind closely to the left, as it does for statements:

for(int col = 0; col < e_col_max; ++col)
  (a)  'int col = 0;'     is an expression-statement
  (b)  'col < e_col_max;' is an expression followed by a semicolon
       '++col'            is an expression
(a) really is a statement. (b) is an oddity: an expression-statement
is defined as an [optional] expression followed by a semicolon--which
describes (b), yet (b) is not a statement.

This choice is consistent with our general practice of writing no space
before the colon on labels:
  case '&': z += "&amp;"; break;
but a space on both sides of it in other cases:
  data = control.GetValue() ? "Yes" : "No"; // ternary operator
  class B0 : private Uncopyable {};         // inheritance
  explicit jdn_t(int d) : value_(d) {}      // initializer
(IMO) because I see the colon in a range-based for statement as more
akin to those other cases: a colon terminates a case label, but it is
a separator otherwise.

* authenticity_test.cpp
    for(auto const& fn: filenames)       <-- 52a
    for(auto const& filename: filenames) <-- 52b
* wx_table_generator.cpp
    for(auto& column: columns_) <-- 52a
    for(auto& ci: columns_)     <-- 52b
et passim

In these two files, only the name of the variable differs. I'm going to
keep the names of the original iterators: both are simply 'i' in these
files, which immediately suggests an iterator. I realize these variables
are no longer iterators, but their purpose is very similar: they're
dereferenced iterators, for lack of a better term. Rationale: I find the
original names clear enough; and this expansive syntactic change is
already risky enough without adding any unnecessary lexical change.

But before that zeroth step, I'll change two files so that this command
  grep 'for.*auto[^m]' *.?pp | sed -e'/&.* : /d'
finds nothing in present HEAD, so that I can use it to make sure I don't
write a ranged-for that lacks "&" or " : ".

> First, I'll [...] Then I'll turn first to [...] and, last, to

As von Moltke the Elder is often loosely quoted: "no plan survives
contact with the enemy". Or as the much funnier Danny Kaye says here:
  https://www.youtube.com/watch?v=RuU9gtsjzww
"But first...". [They put poison in his wine; knowing that, you can
omit the first minute. But first, skip to 2:33, 5:00, and 6:55.]




reply via email to

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