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: Static const integral data members can be initialized? - comp.lang ...... to me that allowing initialization of static const float and double within a ... can be compile time constants, can they be used in switch cases? As template parameters? Draw a Cone - comp.graphics.api.openglWrite a function (with no parameters) that draws a cone of ... 5,0,0.5)) - This function will be called from inside ... template OpenGL program using GLUT ... comp.lang.c++.moderated - page 7... 02:27 PM) I have a C++ class heirarchy that I hide behind a c-api inside a ... any idea on whether Sun is EVER going to get around to implementing template template parameters? Causes of Reference to non-existent field ... - Computer Group... function equalstestb(block) %equalstestb A template for ... attributes of the %% S-function such as ports, parameters ... these methods > %% as local functions within the same ... SWITCH expression must be a scalar or string constant - comp.soft ...SWITCH expression must be a scalar or string constant - comp.soft ... SWITCH expression must be a scalar or string constant ... As template parameters? ... > relevant ... Could anyone give me the spice-mode.el - comp.emacsHi, All I am new to *NIX and I am thinking of writing spice code under Emacs. However, I have no idea of Emacs Lisp. Hence, I could not write a packa... calling member functions from WNPROC callback functions - comp ...... process the callback message here inside the instance } } for reusability you could create a template class to ... glutDisplayFunc passing in parameters to function ... peek() vs unget(): which is better? - comp.lang.c++.moderated ...template< typename charT > std::isxxx( charT ch, locale ... in a character type), and requires two parameters. ... encodings, like UTF-8; the only way to do that within ... Correct way to define iterators - comp.lang.c++.moderated ...}; iterator<> has default typedefs for the other three template parameters, so the above ... struct inside a class ... what is the correct way to define a struct inside a ... Question regarding pragma translate_off/on , synthesis_off/on ...My question is, can we nest the pragmas one inside the ... This was the template used by all vendors, and there is ... used to disable warnings for code which checks parameters ... Exploring Template Template Parameters - Volker Simonis - HomepageThe notation follows the diagrams in . The only extension is that template template parameters inside the class' parameter list are typeset in boldface. C++ Common Knowledge: Template Template Parameters | Template ...This chapter provides a brief rundown of template parameters in C++ with examples to help you keep track of the limitations of C++ templates. For simplicity, let's ... 7/24/2012 1:09:33 PM
|