Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2002-02-22 13:07:37

From: "Herb Sutter" <hsutter_at_[hidden]>
> ...
> 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>

What's wrong returning a value? Can't it be optimized away?

> 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.

It can't be spelled, I'm guessing, because the type of a function
includes it return type, which in the example would be pointer to
function returning pointer to function returning pointer to ...

So don't do this -- unless my guess is wrong.

Boost list run by bdawes at, gregod at, cpdaniel at, john at