Topic: Proposal for a solution of core issue 150


Author: lerdsuwa@users.sourceforge.net (Kriang Lerdsuwanakij)
Date: Sun, 9 Jan 2005 10:09:43 GMT
Raw View
The issue of matching default arguments of a class template
has been discussed in core language issue 150 (Template template
parameters and default arguments), and also the item ES070 in
N1598 document (Allow default arguments for template template
arguments) but there hasn't been a solution so far.

So I propose a solution here based on the variable-length
template parameter list or variadic template idea proposed
elsewhere.  It is applied to the template template parameter
syntax.

In the current standard, the following example indicates that
TT is a template template parameter matching a class template
with exactly one template parameter:

 template <template <class> class TT> void f() {
  TT<int> tt;
 }

My idea would extend the language to allow new syntax:

 template <template <class, ...> class UU> void g() {
  UU<int> uu;
 }

It means that UU matches a class template with at least
one parameter, if there are more than one, all the remaining
must have default arguments.

With this idea, compatibilty with the current standard is
maintained.  Moreover, programmers can decide between two behaviors
on a per template basis.

Some examples:

 template <class T> class C;
 template <class T, class U = int> class D;
 template <class T, class U> class E;

 template <template <class> class TT> void f();
 template <template <class, ...> class TT> void g();

 template <template <class> class TT> void h();        // #1
 template <template <class, class> class TT> void h(); // #2

 template <template <class, ...> class TT> void i();        // #3
 template <template <class, class, ...> class TT> void i(); // #4

 int main() {
  f<C>();   // OK
  f<D>(); f<E>();  // Error - not match
  g<C>(); g<D>();  // OK
  g<E>();   // Error - not match
  h<C>();   // OK - choose #1
  h<D>(); h<E>();  // OK - choose #2
  i<C>();   // OK - choose #3
  i<D>();   // Error - ambiguous between #3, #4
  i<E>();   // OK - choose #4
 }

--Kriang Lerdsuwanakij <lerdsuwa@users.sourceforge.net>

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]