Topic: Template template parameters and defaults
Author: quintus_x@hotmail.com (P G)
Date: Sat, 5 Apr 2003 04:21:09 +0000 (UTC) Raw View
I don't like the way default parameters of templates are handled when
the compiler is matching a template to a formal argument in a template
parameter list. It would be ideal if the following were true.
template<
class T
class Policy_1 = P_1
//...
class Policy_n = P_n
>
class I_take_N_parameters_N_minus_1_of_which_have_default_types{...}
template<
class T
template <class> class TTP_taking_one_type
>
class I_will_compile{
TTP_taking_one_type<T> var;
//....
};
I_will_compile<int,
I_take_N_parameters_N_minus_1_of_which_have_default_types> OK;
///yes!!
I think the current restriction on template template parameters (TTP)
is too draconian and removes much of the contruct's potential power. I
somewhat understand the need to specify the number of arguments the
TTP expects to be instatiated with; the philosophy being that it is
better to catch errors at the template definition rather than at
instatiation. However, if a template can legally be instatiated with
one type (like std::vector) and a TTP expects to be instatiated with
one type why disallow that?
Here is a more realistic example:
template<
class T,
template<class U, class A = std::allocator<U> > class C = std::deque,
template<class> class Ptr = boost::shared_ptr
//...
>
class Manager{
C<Ptr<T> > SharedObjects;
//...
};
Instiantiating it like:
Manager<Sprite, std::list, Loki::SmartPtr> SpriteManager;
is asking for trouble from the compiler. I think that is unfortunate.
I'd have to use a suboptimal and somewhat unsafe workaround such as:
template<class T> class MyLokiSmartPtr : public
Loki::SmartPtr<T>{/*...*/}
//^^Yes, inheritance. There's no way I'm writing all those forwarding
functions
Manager<Sprite, std::list, MyLokiSmartPtr> SpriteManager;
BTW, I found it interesting that Bjarne Stroustrup, in appendix C:13.3
of TC++PL, uses TTPs in exactly the same way the current
interpretation disallows.
I think it would be in the best interest of C++ generic programming to
change the way default parameters of templates and TTPs interact.
IMHO, the current definition is quite unnatural. Am I missing some
terrible ramification of allowing the above?
---
[ 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 ]