Topic: Tricky templates
Author: Jaakko Jarvi <jjarvi@cs.utu.fi>
Date: 1997/01/08 Raw View
Hello,
I tried to consult the draft standard, whether the following template
would be ok, but didn't quite figure it out:
template <int N, int M>=20
X<N+M> f( X<N>, X<M>)=20
{ // ... };
Here, X is a template class having a single template parameter of type
int.=20
On the other hand the draft says, that the template parameter may be
a constant expression of integral type. On the other hand, it denies
a construction like=20
template<int i> void f(A<i+1>)
By stating: Nontype parameters shall not be used in expressions in
the function
declaration.
Then, how about=20
template <class T1, class T1>=20
X<T1::N+T2::N> f( T1, T2)=20
{ // ... };
where T1 and T2 would have N specified as an enum constant. Here nontype
parameters are
not used in expressions in function declaration, so is this legal?
I understand that these kind of template definitions won't propably
compile at any
compiler, at least at the moment, but is the code invalid according to
the future standard?
/Jaakko J=E4rvi
--=20
( Jaakko J=E4rvi E-mail: jjarvi@cs.utu.fi )
( University of Turku Web: www.cs.utu.fi/staff/jjarvi )
( Computer Science Tel: +358 2 333 8656 )
[ 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: comeau@panix.com (Greg Comeau)
Date: 1997/01/10 Raw View
In article <32D39D2F.5608@cs.utu.fi> jjarvi@cs.utu.fi writes:
>I tried to consult the draft standard, whether the following template
>would be ok, but didn't quite figure it out:
>
>template <int N, int M>=20
>X<N+M> f( X<N>, X<M>)=20
>{ // ... };
>
>Here, X is a template class having a single template parameter of type
>int.=20
Tis not a problem.
>On the other hand the draft says, that the template parameter may be
>a constant expression of integral type. On the other hand, it denies
>a construction like=20
>
> template<int i> void f(A<i+1>)
>
>By stating: Nontype parameters shall not be used in expressions in
>the function declaration.
This example is ok too. Deduction on the _call_ would fail and hence
require explicit specifications, but that's another issue completely.
>Then, how about=20
>
>template <class T1, class T1>=20
>X<T1::N+T2::N> f( T1, T2)=20
>{ // ... };
>
>where T1 and T2 would have N specified as an enum constant. Here nontype
>parameters are
>not used in expressions in function declaration, so is this legal?
I don't believe this one is right.
>I understand that these kind of template definitions won't propably
>compile at any
>compiler, at least at the moment, but is the code invalid according to
>the future standard?
The valid ones should compile with Comeau C++ 4.0 with no problem.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009/Fax:718-441-2310
Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com/CIS:72331,3421
---
[ 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 ]