skipping template parameters inside template parameters

  • Follow


I'm not sure how to phrase this, so let me start by showing an
example:

template <class F>
struct Foo1
{
  Foo1() {}
  ~Foo1() {}

  F mInternalThing;
};

template <class F>
struct Foo2
{
  Foo2() {}
  ~Foo2() {}

  F mOtherThing;
};

template <class B>
struct Bar
{
  Bar() {}
  ~Bar() {}

  B mInternalFoo;
};

int main()
{
  Bar<Foo1<int> > bar1;
  Bar<Foo2<int> > bar2;
}

This code compiles and all is dandy. However, I want to be able to do
something like this:

int main()
{
  Bar<Foo1> bar1;
}

and have the template parameters of Foo1 automagically be <int>. This
is because in my particular use case I know that the template
parameter of Bar has to be another template, and I know what the
template parameters of that template should be (int in this example).

I don't want main() to have to worry about that, because it's
essentially an internal detail of Bar. However, I still want Bar to be
a template class, because there are multiple different Foo types that
it can work with.

Is there any way to do this? I'm guessing not, since Foo1 isn't a type
until it has some template parameters. If not, then is there some
other way I should be structuring my code?

Sorry if this isn't clear. I can try to re-explain it if people can't
follow what I'm asking for.
0
Reply kito 9/25/2010 10:41:46 PM

* kito, on 26.09.2010 00:41:
> I'm not sure how to phrase this, so let me start by showing an
> example:
>
> template<class F>
> struct Foo1
> {
>    Foo1() {}
>    ~Foo1() {}
>
>    F mInternalThing;
> };
>
> template<class F>
> struct Foo2
> {
>    Foo2() {}
>    ~Foo2() {}
>
>    F mOtherThing;
> };
>
> template<class B>
> struct Bar
> {
>    Bar() {}
>    ~Bar() {}
>
>    B mInternalFoo;
> };
>
> int main()
> {
>    Bar<Foo1<int>  >  bar1;
>    Bar<Foo2<int>  >  bar2;
> }
>
> This code compiles and all is dandy. However, I want to be able to do
> something like this:
>
> int main()
> {
>    Bar<Foo1>  bar1;
> }
>
> and have the template parameters of Foo1 automagically be<int>. This
> is because in my particular use case I know that the template
> parameter of Bar has to be another template, and I know what the
> template parameters of that template should be (int in this example).
>
> I don't want main() to have to worry about that, because it's
> essentially an internal detail of Bar. However, I still want Bar to be
> a template class, because there are multiple different Foo types that
> it can work with.
>
> Is there any way to do this? I'm guessing not, since Foo1 isn't a type
> until it has some template parameters.

You can always do

   template< template< class > class B >
   struct Bar
   {
       B<int> internalFoo_;
   };


> If not, then is there some
> other way I should be structuring my code?

At least judging from your examples, the code could benefit from restructuring. 
There seems to be nothing in common between Foo1 and Foo2.


Cheers & hth.,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>
0
Reply Alf 9/25/2010 11:51:59 PM


On Sep 25, 4:51=A0pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> wrote:
> * kito, on 26.09.2010 00:41:
>
>
>
>
>
> > I'm not sure how to phrase this, so let me start by showing an
> > example:
>
> > template<class F>
> > struct Foo1
> > {
> > =A0 =A0Foo1() {}
> > =A0 =A0~Foo1() {}
>
> > =A0 =A0F mInternalThing;
> > };
>
> > template<class F>
> > struct Foo2
> > {
> > =A0 =A0Foo2() {}
> > =A0 =A0~Foo2() {}
>
> > =A0 =A0F mOtherThing;
> > };
>
> > template<class B>
> > struct Bar
> > {
> > =A0 =A0Bar() {}
> > =A0 =A0~Bar() {}
>
> > =A0 =A0B mInternalFoo;
> > };
>
> > int main()
> > {
> > =A0 =A0Bar<Foo1<int> =A0> =A0bar1;
> > =A0 =A0Bar<Foo2<int> =A0> =A0bar2;
> > }
>
> > This code compiles and all is dandy. However, I want to be able to do
> > something like this:
>
> > int main()
> > {
> > =A0 =A0Bar<Foo1> =A0bar1;
> > }
>
> > and have the template parameters of Foo1 automagically be<int>. This
> > is because in my particular use case I know that the template
> > parameter of Bar has to be another template, and I know what the
> > template parameters of that template should be (int in this example).
>
> > I don't want main() to have to worry about that, because it's
> > essentially an internal detail of Bar. However, I still want Bar to be
> > a template class, because there are multiple different Foo types that
> > it can work with.
>
> > Is there any way to do this? I'm guessing not, since Foo1 isn't a type
> > until it has some template parameters.
>
> You can always do
>
> =A0 =A0template< template< class > class B >
> =A0 =A0struct Bar
> =A0 =A0{
> =A0 =A0 =A0 =A0B<int> internalFoo_;
> =A0 =A0};
>
> > If not, then is there some
> > other way I should be structuring my code?
>
> At least judging from your examples, the code could benefit from restruct=
uring.
> There seems to be nothing in common between Foo1 and Foo2.
>
> Cheers & hth.,
>
> - Alf
>
> --
> blog at <url:http://alfps.wordpress.com>

Thank you, that is exactly what I was looking for! Just as a note, the
lack of commonality between Foo1 and Foo2 is just because of this
simplified example. In the actual code Foo1 and Foo2 are both derived
from a common base.

Thanks again,
-kito
0
Reply thekito 9/26/2010 12:04:36 AM

2 Replies
152 Views

(page loaded in 0.062 seconds)

Similiar Articles:













7/24/2012 1:09:33 PM


Reply: