skipping template parameters inside template parameters

K

kito

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.
 
A

Alf P. Steinbach /Usenet

* 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
 
K

kito

* kito, on 26.09.2010 00:41:


















You can always do

   template< template< class > class B >
   struct Bar
   {


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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top