Topic: template nontype parameter expressions
Author: Larry J Evans <evansl@cs.tamu.edu>
Date: 1996/06/23 Raw View
// For the reasons given in the code comments below, I believe the
// following code is not allowed by the April95 working paper. Is the
// reasoning shown below correct? If the code is not allowed, then I
// wonder why, since type parameters are allowed in
// expressions(e.g. T* in [temp.point]para 12), and I'd guess nontype
// expressions would be no more difficult to compile than type
// expressions.
template<class T,int Rank> class Array
{
public:
Array<T,Rank-1> operator[](int);
// This is not allowed by 14.10.2 [temp.deduct] para 10, which
// says "Nontype parameters shall not be used in expressions in
// the function declaration".
// But [temp.deduct]para 10 was for FUNCTION templates. It may be
// different for CLASS templates.
// On the other hand, 14.11 [temp.mem.func] para 1 says "A member
// function of a template class is implicitly a template function
// with the "template-parameters" of its class as its
// "template-parameters".
// On the other hand, section 14.3.2[temp.point] para 10 does
// indicate "recursive" FUNCTION templates are allowed. However,
// the recursion was in the definition, not the declaration.
// However, [temp.point]para 12 implies that recursion on the type
// parameter is allowed(e.g. parameter T used as T*), but that may
// result in infinite recursion. The implementation may report
// such a declaration as being ill-formed.
// Now if recursion on a type parameter(e.g. T* in
// [temp.point]para 12) is allowed, why wouldn't recursion on a
// non-type parameter be allowed(Rank-1)? Infinite recursion
// could be avoided by a specialization ([temp.point]para 10 or
// see below).
};
template<class T> class Array<T,1>
//Avoid infinite recursion by a specialization similar to section
//14.6[temp.class.spec] para 3 #4
{
public:
T& operator[](int i);
};
int
main(void)
{ Array<int,1> vec
//14.6.1[temp.class.spec.match] para 4 requires 2 args.
; Array<int,2> mat
; mat[0][0]=vec[0]
; return 0
;}
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/06/24 Raw View
>>>>> "LJE" == Larry J Evans <evansl@cs.tamu.edu> writes:
[...]
LJE> template<class T,int Rank> class Array
LJE> {
LJE> public:
LJE> Array<T,Rank-1> operator[](int);
LJE> // This is not allowed by 14.10.2 [temp.deduct] para 10, which
LJE> // says "Nontype parameters shall not be used in expressions in
LJE> // the function declaration".
[...]
I don't have a April 95 draft handy to go through the legalese, but
AFAIK this has always been OK (except for the partial specialization
required to stop the recursion). Variants of this scheme have been
discussed both on Usenet and in print (e.g., the book by Nackman &
Barton) --- I doubt they were illegal at the time.
Since then however, non-type arguments for function templates have
been added to the C++ language and hence I think you'll have no
trouble at all justifying this approach.
Daveed
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Nathan Myers <ncm@cantrip.org>
Date: 1996/06/24 Raw View
Larry J Evans wrote:
> [Re: use of non-type template parameters]
> // Now if recursion on a type parameter(e.g. T* in
> // [temp.point]para 12) is allowed, why wouldn't recursion on a
> // non-type parameter be allowed(Rank-1)? Infinite recursion
> // could be avoided by a specialization ...
Use of non-type template parameters has become far
freer since early '95. You won't see this implemented in
any compilers yet, but nontype parameters are allowed basically
anywhere. Function templates may be called as (e.g.) "f<int,1>()"
to specify those parameters where they can't be deduced.
As for recursive definitions -- this technique has been used
with great beauty and power by Todd Veldhuizen; see his web page
http://monet.uwaterloo.ca/blitz/
Nathan Myers
ncm@cantrip.org
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]