sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Miguel Guedes's Avatar

On template parameters


Question posted by: Miguel Guedes (Guest) on August 26th, 2007 04:55 PM
Hello,

I have template class definition like so:

template <typename Type>
class Foo
{
4 Answers Posted
Victor Bazarov's Avatar
Guest - n/a Posts
#2: Re: On template parameters

Miguel Guedes wrote:
Quote:
Originally Posted by
I have template class definition like so:
>
template <typename Type>
class Foo
{
.
Quote:
Originally Posted by
>>

.
template <typename SubTypeType* create();
template <typename SubType, typename Param1Type* create();
template <typename SubType, typename Param1, typename Param2Type*
create(); .
Quote:
Originally Posted by
>>

.


Seems like those functions are missing some arguments.
Quote:
Originally Posted by
};
>
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


=?ISO-8859-1?Q?Erik_Wikstr=F6m?='s Avatar
=?ISO-8859-1?Q?Erik_Wikstr=F6m?= August 26th, 2007 05:25 PM
Guest - n/a Posts
#3: Re: On template parameters

On 2007-08-26 17:47, Miguel Guedes wrote:
Quote:
Originally Posted by
Hello,
>
I have template class definition like so:
>
template <typename Type>
class Foo
{
.
:
.
template <typename SubTypeType* create();
template <typename SubType, typename Param1Type* create();
template <typename SubType, typename Param1, typename Param2Type* 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 = intType* create();

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

template <typename T, typename U = MyType<T

--
Erik Wikström
Miguel Guedes's Avatar
Guest - n/a Posts
#4: Re: On template parameters

Victor Bazarov wrote:
Quote:
Originally Posted by
Miguel Guedes wrote:
Quote:
Originally Posted by
>I have template class definition like so:
>>
>template <typename Type>
>class Foo
>{
>.
>.
>template <typename SubTypeType* create();
>template <typename SubType, typename Param1Type* create();
>template <typename SubType, typename Param1, typename Param2Type*
>create(); .
>.

>
Seems like those functions are missing some arguments.
>
Quote:
Originally Posted by
>};
>>
>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?


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.


--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me by email,
remove the X from my address. -
Miguel Guedes's Avatar
Guest - n/a Posts
#5: Re: On template parameters

Miguel Guedes wrote:
Quote:
Originally Posted by
template <typename Type>
class Foo
{
.
:
.
template <typename SubTypeType* create();
template <typename SubType, typename Param1Type* create();
template <typename SubType, typename Param1, typename Param2Type* create();
.
:
.
};


That should actually be:


template <typename Type>
class Foo
{
 
Not the answer you were looking for? Post your question . . .
197,034 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,034 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors