<br><br><div class="gmail_quote">Le 4 janvier 2012 15:00, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>></span> a écrit :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mon, January 2, 2012 19:48, John Bytheway wrote:<br>
> I think you're conflating two new language features that ought to be<br>
> considered in isolation:<br>
><br>
> 1. Automatic polymorphization of parameter types.<br>
> 2. Automatic deduction of return types for non-lambda functions.<br>
><br>
> Thinking about how to add feature 1 in isolation, in a way that works<br>
> for all functions, function templates, and lambda functions, without the<br>
> above-mentioned ambiguity between parameters without names and those without<br>
> types, the solution that first occurs to me is the one suggested by Robert<br>
> Frunzke in a comment on your C++Next article: use "auto" in<br>
> place of the type name rather than omitting it.<br>
><br>
> So we would write<br>
><br>
> auto min(auto x, auto y)->decltype(x < y ? x : y) { return x < y ? x : y; }<br>
><br>
> which is equivalent to<br>
><br>
> template <class T, class U> auto min(T x, U y)->decltype(x < y ? x : y) {<br>
> return x < y ? x : y; }<br>
<br>
[FYI, this function is broken: ?: produces an lvalue if its second and third<br>
arguments are both lvalues, and decltype of an lvalue (outside the<br>
decltype(identifier) case) produces a reference. So this returns a reference<br>
to a function argument.]<br>
<br>
I'd be somewhat wary of overloading the meaning of 'auto' in this way. Another<br>
likely proposal for 'auto' extension is allowing 'auto' anywhere within a<br>
variable's type, and deducing the appropriate type from the initializer.<br>
Hence:<br>
<br>
auto (&min)(auto x, auto y) = std::min<int>;<br>
<br>
The 'auto' in the parameter types here would naturally mean 'deduce this type<br>
from the initializer'. Another potential source of confusion would be with<br>
default arguments:<br>
<br>
auto f(auto x = 0) { return x; }<br>
<br>
Is this implicitly a function template, or does x have type 'int'?<br>
<br>
Perhaps 'template' would be a better keyword to propose than 'auto' here?<br>
<br>
auto min(template a, template b) -> std::remove_reference<decltype(a < b ? a :<br>
b)> noexcept(noexcept(a < b ? a : b)) { return a < b ? a : b; }<br>
<br>
... or more tersely ...<br>
<br>
auto min(template a, template b) noexcept(auto) { return a < b ? a : b; }<br>
<br>
- Richard<br></blockquote><div><br>Hi Richard,<br><br>I really like the later syntax, very terse, however how does it address the binding temporary to reference issue ?<br><br>Do we rely on the compiler deducing that the reference is not adequate and removing it ?<br>
<br><br>As for the "same type" discussion (ie, do we consider that    auto min(template a, template b)    have similar or different types for a and b), I would simply rely on decltype:<br><br>> auto min(template a, decltype(a) b)   both a and b have the same type<br>
<br>> auto min(template a, template b)   a and b have possibly different type<br><br><br>I really wish this could get reality, the kludgy syntax has been a trademark of C++ for too long :)<br><br>-- Matthieu<br></div></div>