Topic: Template with partially bound arguments as template template-argument


Author: Michael Kenzel <dot@aon.at>
Date: Mon, 7 Sep 2009 09:26:16 CST
Raw View
Hi,

I was just thinking that it could be useful if you'd be able to
partially bind some arguments of a class template and use the result
as a template template-argument, like this for example:

template <class A, class B>
struct blub
{
};

template <class A, template<class> class Base>
struct test : public Base<A>
{
};

test<int, template<class A> struct blub<A, int> > x;  // not possible
atm

I could even think of using one of those as default argument:

template <class A, template<class> class Base = template<class A>
struct blub<A, int> >
struct test : public Base<A>
{
};

While not being essential, I think this could be useful. Anyway, I
guess there might be some good reasons why this isn't possible that I
just didn't think of so far, maybe somebody can tell me!?

I think the upcoming template aliases enable us to do something like
that but would require introducing a new name for the specialized
type...

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Abrahams <dave@boostpro.com>
Date: Tue, 8 Sep 2009 00:53:21 CST
Raw View
on Mon Sep 07 2009, Michael Kenzel <dot-AT-aon.at> wrote:

> Hi,
>
> I was just thinking that it could be useful if you'd be able to
> partially bind some arguments of a class template and use the result
> as a template template-argument, like this for example:
>
> template <class A, class B>
> struct blub
> {
> };
>
> template <class A, template<class> class Base>
> struct test : public Base<A>
> {
> };
>
> test<int, template<class A> struct blub<A, int> > x;  // not possible
> atm
>
> I could even think of using one of those as default argument:
>
> template <class A, template<class> class Base = template<class A>
> struct blub<A, int> >
> struct test : public Base<A>
> {
> };
>
> While not being essential, I think this could be useful. Anyway, I
> guess there might be some good reasons why this isn't possible that I
> just didn't think of so far, maybe somebody can tell me!?
>
> I think the upcoming template aliases enable us to do something like
> that but would require introducing a new name for the specialized
> type...

MPL lambda expressions can do that:

   template <class A, class MakeBase>
   struct test : public mpl::apply<MakeBase, A>::type
   {
       ...
   };

   test<A, blub<_1,int> > x;

and:

   template <class A, class MakeBase = blub<_1,int> >
   struct test : public mpl::apply<MakeBase, A>::type
   {
       ...
   };

See http://boost.org/libs/mpl

HTH,
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Tue, 8 Sep 2009 00:53:48 CST
Raw View
On 7 sep, 17:26, Michael Kenzel <d...@aon.at> wrote:
> Hi,
>
> I was just thinking that it could be useful if you'd be able to
> partially bind some arguments of a class template and use the result
> as a template template-argument, like this for example:
>
> template <class A, class B>
> struct blub
> {
>
> };
>
> template <class A, template<class> class Base>
> struct test : public Base<A>
> {
>
> };
>
> test<int, template<class A> struct blub<A, int> > x;  // not possible
> atm

I suggest you take a look at Boost.MPL, which provides that kind of
facilities in C++03.
You can do partial meta-function application, meta-function
composition, write lambda expressions with placeholders, etc.

(A meta-function is a template that takes N types and returns one as a
member typedef named "type", like type traits)


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Tue, 8 Sep 2009 00:56:17 CST
Raw View
On 7 Sep., 17:26, Michael Kenzel <d...@aon.at> wrote:
>
> I was just thinking that it could be useful if you'd be able to
> partially bind some arguments of a class template and use the result
> as a template template-argument

You will like the new type alias syntax that C++0x offers and supports
templates as well:

   using blah = int; // like  typedef int blah;

   template<class A, class B> class blah;

   template<class A>
   using blahAint = blah<A,int>;

blahAint can be used as a template<class> template parameter as far as
I know.

Cheers!
SG

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]