Re: Ignoring top-level restrict qualifiers for the purpose of
template argument deduction
Consider the following example, in which it is assumed that
restrict is ignored for template argument deduction. (Template
mycopy is included for completeness, but has the same semantics
as copy.)
template <class InIter, class OutIter>
OutIter mycopy(InIter s, InIter end, OutIter d)
{
for ( ; s != end; ++d, ++s) *d = *s;
return d;
}
void f(int * restrict p, int * restrict q, int n) {
// restrict promotes optimization of next loop
for(int i=1; i<n; i++) p[i] += q[i-1];
// source and destination do not overlap, so call the
// specialization with parameter d of type: int * restrict
mycopy<int *, int * restrict>(p, p+n, q);
// source and destination overlap, so call
// specialization with parameter d of type: int *
mycopy(q+1, q+n, q);
}
Observations:
1. Just because it is appropriate and useful for p and q to be
restrict-qualified for the operations in f does not imply
that in every function call for which they are arguments,
the corresponding parameters should be restrict-qualified.
2. Explicit template argument specification gives us the means
to call a specialization of mycopy with a restrict-qualified
parameters when it is appropriate, as in the first call.
3. Ignoring the restrict qualifier for template argument deduction
means that deduction gives the safe version, with unqualified
parameters, as in the second call. That seems better than the
other way around.
4. A technical difficulty with trying to use restrict for
argument deduction is illustrated by the first call. The
first argument, p, is an lvalue that is restrict-qualified,
but the second, p+1, is a value that is not. What
qualification would be deduced for InIter in this case?
(You might suggest that the rule take into account that the
second argument is based on a restrict-qualified pointer,
but you will also have to deal with cases in which it is
not. And in any event, it would be a significant burden on
an implementation to require it to make that analysis in the
general case.)
>From this, it seems clear to me that restrict should be ignored
for template argument deduction, and that explicitly specifying
template arguments provides an appropriate means to produce
specializations with restrict-qualified parameters when needed.
In addition, a previously noted feature of this approach is that
it allows restrict to be macro-defined to null. Not only does
this promote portability to implementations that do not support
the qualifier, but it also gives the programmer an easy way to
check whether a program bug is the result of a non-conforming
use of the restrict qualifier. I agree that these should not be
considered decisive, but they are practical advantages
nevertheless.
-- Bill Homer (651) 683-5606 Cray Inc. homer@cray.com 655F Lone Oak Drive, Eagan, MN 55121--------------------- Object Oriented Numerics List -------------------------- * To subscribe/unsubscribe: use the handy web form at http://oonumerics.org/oon/ * If this doesn't work, please send a note to owner-oon-list@oonumerics.org
This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:12 EST