[cfe-dev] Help getting started
Richard Smith
richard at metafoo.co.uk
Wed Jan 4 06:00:46 PST 2012
On Mon, January 2, 2012 19:48, John Bytheway wrote:
> I think you're conflating two new language features that ought to be
> considered in isolation:
>
> 1. Automatic polymorphization of parameter types.
> 2. Automatic deduction of return types for non-lambda functions.
>
> Thinking about how to add feature 1 in isolation, in a way that works
> for all functions, function templates, and lambda functions, without the
> above-mentioned ambiguity between parameters without names and those without
> types, the solution that first occurs to me is the one suggested by Robert
> Frunzke in a comment on your C++Next article: use "auto" in
> place of the type name rather than omitting it.
>
> So we would write
>
> auto min(auto x, auto y)->decltype(x < y ? x : y) { return x < y ? x : y; }
>
> which is equivalent to
>
> template <class T, class U> auto min(T x, U y)->decltype(x < y ? x : y) {
> return x < y ? x : y; }
[FYI, this function is broken: ?: produces an lvalue if its second and third
arguments are both lvalues, and decltype of an lvalue (outside the
decltype(identifier) case) produces a reference. So this returns a reference
to a function argument.]
I'd be somewhat wary of overloading the meaning of 'auto' in this way. Another
likely proposal for 'auto' extension is allowing 'auto' anywhere within a
variable's type, and deducing the appropriate type from the initializer.
Hence:
auto (&min)(auto x, auto y) = std::min<int>;
The 'auto' in the parameter types here would naturally mean 'deduce this type
from the initializer'. Another potential source of confusion would be with
default arguments:
auto f(auto x = 0) { return x; }
Is this implicitly a function template, or does x have type 'int'?
Perhaps 'template' would be a better keyword to propose than 'auto' here?
auto min(template a, template b) -> std::remove_reference<decltype(a < b ? a :
b)> noexcept(noexcept(a < b ? a : b)) { return a < b ? a : b; }
... or more tersely ...
auto min(template a, template b) noexcept(auto) { return a < b ? a : b; }
- Richard
More information about the cfe-dev
mailing list