|
Boost : |
From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2002-02-21 12:15:55
----- Original Message -----
From: "Peter Dimov" <pdimov_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, February 21, 2002 1:28 PM
Subject: Re: [boost] More on auto
> From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> > From: "Peter Dimov" <pdimov_at_[hidden]>
> > > From: "Fernando Cacciola" <fcacciola_at_[hidden]>
> > > > I don't think that the very small increased complexity of requiring
> auto
> > > > declarations to have a type name are not worth the increased safety.
> > > > Don't you think it does improves safety a lot?
> > >
> > > No, actually, I don't. Partly because the right time to use auto
> > > declarations is exactly when the actual type doesn't matter, and
partly
> > > because I don't see how requiring a dummy identifier would improve
> safety
> > > somehow.
> > >
> > I think your are assuming too much about how 'auto' would end up being
> used.
>
> No, I don't assume anything.
>
> > Unless you are considering that auto variables MUST only be used in very
> > restrictive contexts (in which way I'd like to see how that would be
> > enforced);
>
> By a coding standard, presumably. You can already write unreadable code if
> you want to.
>
> > by the same reasoning by which you would be comfortable with
> > type-nameless auto variables you would be just the same comfortable with
> > type nameless template arguments:
> >
> > That is, it appears from your arguments that this:
> >
> > template<class,class,class> foo(a,b,c)
> > {
> > d = a + b -c ;
> > return d > 0 ;
> > }
> >
> > isn't any more safe than this:
> >
> > template<class T,class U,class V> bool foo(T a,U b,V c)
> > {
> > typeof(T+U-V) d = a + b -c ;
> > return d > 0 ;
> > }
>
> I fail to see the analogy or the connection with "my arguments."
>
> Please answer the question above, which is: how, exactly does this:
>
> auto X x = expr;
>
> provide more safety than
>
> auto x = expr;
>
The former attaches the name 'X' to the type of 'x'. The later doesn't.
The fact that 'x' has a type with a *distinct* name allows the programmer to
infer the nature of the expressions involving x.
If you focus on the declaration along you'll miss the point. Is the
interaction of x with other objects which benefits from having a named type.
For example:
auto x = expr1 ;
auto y = expr2 ;
(x < y )
The latter expression can involve an implicit conversion.
The compiler won't tell me that in all cases, for example, if x is int and y
is double.
But, in
auto T x = expr1 ;
auto U y = expr2 ;
(x < y)
since I intentionally used the type names T and U to x and y, the
expression, even wrote the same way, expresses explicitely the fact that it
could incurr implicit conversions.
I might know in advance that expr1 and expr2 are of the same type, in which
case I would write:
auto T x = expr1 ;
auto T y = expr2 ;
If they are not of the same type, the compiler will give an error in the
second declaration.
So the next line:
(x < y )
can be safely left as it this being confident that no conversions would
ocurr.
As Herb pointed out, I can achieve the same if I additionally use typeof(),
but then...
If auto is allowed to be nameless, than transitively, complex epxressions
would be allowed to involve different objects with nameless,
(non-diferenciable) types.
Then, we end up with a type system than at the same time that the compiler
mantains type safety under the hoods, the programmer knows nothing about it.
That's the analogy with nameless template arguments:
We could, strictly speacking, allow template classes and functions to omit
the type:
template<class,class> bool less(x,y) { return x < y ; }
This would have just the exact same semantics that auto declarations and
expressions involving auto-declared variables.
However,
template<class T,class U> bool less(T x, U y ) { return x < y ; }
template<class T,class T> bool less(T x, T y ) { return x < y ; }
are expressively different: the first involves different types, the second
the same type. It is not a trivial difference.
That is, if you think beyond the mere declaration of 'auto x = expr', and
think about how will x interact in expressions involving other auto
variables, you can see that allowing them to be type-nameless is the same as
allowing template fucntions/classes to omit the type.
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk