Re: Suggestion for N1984 auto proposal
On Wed, 19 Jul 2006 16:26:13 -0600, Gene Bushuyev wrote:
No, auto is most necessary in everyday code as in
for( auto i = c.begin(); i != c.end(); ++i ) { ... }
Now tell me, why? Why is it necessary in everyday code to save a few keystrokes
and lose clarity and thus to make coding more error-prone?
There is no loss: see below:
for(std::vector<auto,auto>::const_iterator i = c.begin(); i != c.end(); ++i )
{ ... }
This is not difficult to type,
You're kidding right?
you do realise many advanced language *heavily* emphasise
the advantage of complete type inference? I'm not advocating
that .. and neither is the 'auto' proposal.
Instead, auto variables allow you to factor and simplify
statements such as:
int i = f (g (x) );
into
auto tmp = g (x);
int i = f (tmp);
In this case you can see clearly there is no loss of
an explicit type annotation .. because in the original
form the subexpression 'g (x)' did not posses an
explicit type annotation in the first place.
In the case of a loop:
for( auto i = c.begin(); i != c.end(); ++i ) {
float v = *i; other.push_back(v); ...
}
offers major advantages in 'cut and paste' genericity,
including templates, precisely because it does NOT name
the type. In particular, this body of code is
polyadic (aka functorially polymorphic or generic) which
means it is independent of the container type of c. We do
not CARE what the type of either the container or iterator
is .. all we care about is that the algorithm iterates over
the values of the container. This means we can easily
make the code work when C is a vector.. and replace it
later with a list without modifications.
Roughly speaking this extension would reduce the need
for crud in STL containers like:
class Container .. { typedef ... iterator_type; .. }
which is needed mainly to compensate for the *lack*
of type deduction.
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]