Boost logo

Boost :

From: Herb Sutter (hsutter_at_[hidden])
Date: 2002-02-22 00:03:10


> The exact semantics of the auto declaration depend on its intended use.

Could you amplify on that sentence?

> In the common case where auto declarations are used to introduce named
> temporaries [...] we don't need the temporary to be a reference, or be
> const.

So does the first sentence mean the semantics of auto should depend on whether
it's an automatic variable declaration (for any reason, not just named
temporaries) vs. a return type? Or that it depends on what the programmer is
trying to achieve in a given variable declaration (much scarier)?

Turning to auto return types: Let's assume that auto always deduces the
identical type that a template parameter would. Then let's look again at the
following cases of return type deduction, which I think were mentioned in
passing as good uses for auto:

1. "The perfect max()." The idea was:

  template<typename T, typename U>
  auto max( T& t, U& u ) { return u>v ? u : v; }

Putting aside the question of whether the return value is reference or not
(wouldn't the reference be stripped off? perhaps we should allow returning
"auto&" to solve this?), this doesn't actually work with the proposed auto,
does it? The return type can't be deduced at compile time. <minefield> Or was
auto intended to yield a runtime result? If so, I'm not going there.
</minefield>

2. "Function returning pointer to self." The idea was:

  auto func() { return &func; }

This also looked good to me, until it was pointed out that this also requires
an extension to the type system, because the type of the function can't be
spelled in C++98. So auto alone doesn't do this one.

Herb


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk