Re: Easier template declaration with auto keyword
Joe Smith wrote:
There are a few problems.
The first is that this syntax makes it hard to notice that what looks to
be a function definition is actually a function template definition.
The next is that the syntax provides no way to specify that 'a' and 'b'
must have the same type, which is a requirement for many function
templates.
Next up, this makes it a bit more difficult to specify the types of
local variables used in the function. 'auto' can be used in some cases,
but in others it would require extensive use of 'decltype(a)' and
'decltype(b)'.
When I gave some thought to the OP's suggestion, trying to move from a general
feeling of uneasiness to specifics, these were the exact same issues that jumped
out at me foremost, and your analysis for the last is the one that I arrived at,
and not 'happily'. Although none of these issues are intrinsically counter to
the overall feasibility of the proposal, excepting perhaps the second, I
nevertheless think that they argue against the general idea all the same.
Yet on top of these, there are others that are perhaps more damaging. For
instance, C++0x will allow default template arguments for function templates.
How would these be specified in the proposed syntax? Again, how does the syntax
apply to template template parameters? How do we write the following
void foo(auto<auto, std::allocator<?>>&);
so that it can be instantiated by vectors and lists alike? This is not to
mention how we knit non-type template parameters into the equation. Do we permit
awkward "mixed" usages, such as:
template<int I>
int foo(auto a, auto b);
and, if we do, how do we order an explicitly-specified template argument list
when we are required to? Is this:
foo<7, int, char>(1, 'c');
or
foo<int, char, 7>(1, 'c'); ?
Taking this yet further, how do we control viable instantiations using type_traits?
Do we allow the same usage in class member functions? If not, I would argue that
we are introducing something of an awkward distinction. If so, then how do re-write:
template<typename T>
struct A {
A(T t);
A& operator=(T t);
};
and have the 't's constrained to the same type (which is an extension of the
second point you raised in relation to the OPs example, above)? Even given perhaps:
struct A {
A(auto t);
A& operator=(auto t);
};
how do we provide a partial specialisation of this for pointer types? Following
it through consistently, do we permit:
struct A {
A(int t);
A& operator=(int t);
};
to be an explicit specialisation, and if so, how does the compiler know it is
so, and not just an ODR infringement?
All in all, I would say that my uneasiness is not lessened the more I think
about it; in fact, the opposite.
Regards
Paul Bibbings
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]