On template parameters

M

Miguel Guedes

Hello,

I have template class definition like so:

template <typename Type>
class Foo
{
.
:
.
template <typename SubType> Type* create();
template <typename SubType, typename Param1> Type* create();
template <typename SubType, typename Param1, typename Param2> Type* create();
.
:
.
};

The aim is to allow Foo::create to instantiate any (Type) class and let it
construct it with either no construction parameters, one or two.

Is there any way these three methods can be somehow simplified into one while
keeping the same functionality?
 
V

Victor Bazarov

Miguel said:
I have template class definition like so:

template <typename Type>
class Foo
{
.
.
template <typename SubType> Type* create();
template <typename SubType, typename Param1> Type* create();

.

Seems like those functions are missing some arguments.
};

The aim is to allow Foo::create to instantiate any (Type) class and
let it construct it with either no construction parameters, one or
two.

Is there any way these three methods can be somehow simplified into
one while keeping the same functionality?

Don't confuse simplification with reducing the number of member
functions.

Try implementing it to the best of your abilities. See what it leads
to. Then see if you can determine any commonality beween them, and
if you can, see if you can extract it into a separate function or
function template.

BTW, what is the point of having a generic factory like that? What
problem are you solving?

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello,

I have template class definition like so:

template <typename Type>
class Foo
{
.
:
.
template <typename SubType> Type* create();
template <typename SubType, typename Param1> Type* create();
template <typename SubType, typename Param1, typename Param2> Type* create();
.
:
.
};

The aim is to allow Foo::create to instantiate any (Type) class and let it
construct it with either no construction parameters, one or two.

Is there any way these three methods can be somehow simplified into one while
keeping the same functionality?

You can use default parameters:

template <typename SubType, typename Param1 = int> Type* create();

you can even use the other parameters if you want a parametrised type to
be used:

template <typename T, typename U = MyType<T> >
 
M

Miguel Guedes

Victor said:
Seems like those functions are missing some arguments.


Don't confuse simplification with reducing the number of member
functions.

Try implementing it to the best of your abilities. See what it leads
to. Then see if you can determine any commonality beween them, and
if you can, see if you can extract it into a separate function or
function template.

BTW, what is the point of having a generic factory like that? What
problem are you solving?

It's a factory that will do resource management of different polymorphic types,
each of which have different specializations. In other words, I'm developing a
factory for /Type/ types, which must support instantiation of any /Type/'s
/SubType/ types while allowing for up to 2 parameters to be passed in for
construction or none.

In all honesty, I started this thread thinking C++ allowed for template
parameters to be declared as optional. If it did, I'd only need _one_ create
method rather than three, hence my referring to the "simplification of the
methods into one" in my post.

I now realize how stupid that was. That's what you get when have too much
caffeine and not enough sleep I guess.

Thanks for your reply Victor and apologies for misleading you.


PS: After writing this I think I'm going to change Type and SubType to
BaseClass and InstantiateType, respectively, as it doesn't /really/ make much
sense.
 
M

Miguel Guedes

Miguel said:
template <typename Type>
class Foo
{
.
:
.
template <typename SubType> Type* create();
template <typename SubType, typename Param1> Type* create();
template <typename SubType, typename Param1, typename Param2> Type* create();
.
:
.
};

That should actually be:


template <typename Type>
class Foo
{
.
:
.
template <typename SubType> Type* create();
template <typename SubType, typename Param1> Type* create(Param1);
template <typename SubType, typename Param1, typename Param2> Type*
create(Param1, Param2);
.
:
.
};


Apologies for the typo.
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top