Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default type-value for template parameter type

Reply
Thread Tools

Default type-value for template parameter type

 
 
Ragnar
Guest
Posts: n/a
 
      09-03-2003
Hello

I have a template in my kernel slab allocator, used to created object
caches.

template<class T, class A = void>
class ObjectCache
{
public:
explicit ObjectCache(char * name,mword Flags=0,mword
Alignment=0,mword ObjSize=0)
{
...
}


T * Alloc()
{
void * place = Cache->GetObject();
T * x = new (place) T;
return x;
}
T * Alloc(A CtorArg)
{
void * place = Cache->GetObject();
T * x = new (place) T(CtorArg);
return x;
}
private:
...
};

This is an object cache interface used to create kernel objects. The
objects can
take eithe one ctor arg or none, but not more than one.

The two Alloc() methods are for creating objects which have a default
ctor and for those that do not. Now this worked fine in my RH-7 gcc
2.96, but i recently upgraded to RH-9 which has gcc 3.2.2 and it
started giving errors for those instatiations which do not specify the
second type.

For example, the following instantiation compiles fine:

ObjectCache<Lwp, KrThread *> LwpCache("LWPCache");

The second type is specified here as KrThread

But this does not:

ObjectCache<KrThread> KrThreadCache("KernelThreadCache", KT_ALIGN);

gcc gives the error: invalid parameter type `void'

What is happening and how can i correct this ? Also, is there a
mechanism to pass any number of arguments to the ctors. That is
flexibility in the number of arguments.


Regard & Thanks

---
Ragnar
Capital Kernel Engineering
www.capitalos.4t.com
 
Reply With Quote
 
 
 
 
Renato Battaglia
Guest
Posts: n/a
 
      09-03-2003
Hi...

Just change "template<class T, class A = void>" into
"template<class T, class A = void*>"

As your calling code seems to be always passing POINTER_TO_OBJECT
into the template, guess this will keep semantics consistent.


http://www.velocityreviews.com/forums/(E-Mail Removed) (Ragnar) wrote in message news:<(E-Mail Removed). com>...
> Hello
>
> I have a template in my kernel slab allocator, used to created object
> caches.
>
> template<class T, class A = void>
> class ObjectCache
> {
> public:
> explicit ObjectCache(char * name,mword Flags=0,mword
> Alignment=0,mword ObjSize=0)
> {
> ...
> }
>
>
> T * Alloc()
> {
> void * place = Cache->GetObject();
> T * x = new (place) T;
> return x;
> }
> T * Alloc(A CtorArg)
> {
> void * place = Cache->GetObject();
> T * x = new (place) T(CtorArg);
> return x;
> }
> private:
> ...
> };
>
> This is an object cache interface used to create kernel objects. The
> objects can
> take eithe one ctor arg or none, but not more than one.
>
> The two Alloc() methods are for creating objects which have a default
> ctor and for those that do not. Now this worked fine in my RH-7 gcc
> 2.96, but i recently upgraded to RH-9 which has gcc 3.2.2 and it
> started giving errors for those instatiations which do not specify the
> second type.
>
> For example, the following instantiation compiles fine:
>
> ObjectCache<Lwp, KrThread *> LwpCache("LWPCache");
>
> The second type is specified here as KrThread
>
> But this does not:
>
> ObjectCache<KrThread> KrThreadCache("KernelThreadCache", KT_ALIGN);
>
> gcc gives the error: invalid parameter type `void'
>
> What is happening and how can i correct this ? Also, is there a
> mechanism to pass any number of arguments to the ctors. That is
> flexibility in the number of arguments.
>
>
> Regard & Thanks
>
> ---
> Ragnar
> Capital Kernel Engineering
> www.capitalos.4t.com

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      09-04-2003
On 3 Sep 2003 03:29:23 -0700, (E-Mail Removed) (Ragnar) wrote:

>Hello
>
>I have a template in my kernel slab allocator, used to created object
>caches.
>
>template<class T, class A = void>


Drop the second param:

template <class T>

>class ObjectCache
>{
> public:
> explicit ObjectCache(char * name,mword Flags=0,mword
>Alignment=0,mword ObjSize=0)
> {
> ...
> }
>
>
> T * Alloc()
> {
> void * place = Cache->GetObject();
> T * x = new (place) T;
> return x;
> }
> T * Alloc(A CtorArg)
> {
> void * place = Cache->GetObject();
> T * x = new (place) T(CtorArg);
> return x;
> }


Make the above a template member:

template <class A>
T* Alloc(A CtrArg)
{
}

//and, optionally:
template <class A, class B>
T* Alloc(A arg1, B arg2)
{
}

//etc.


> private:
> ...
>};
>
>This is an object cache interface used to create kernel objects. The
>objects can
>take eithe one ctor arg or none, but not more than one.
>
>The two Alloc() methods are for creating objects which have a default
>ctor and for those that do not. Now this worked fine in my RH-7 gcc
>2.96, but i recently upgraded to RH-9 which has gcc 3.2.2 and it
>started giving errors for those instatiations which do not specify the
>second type.


Right, since the signature
T * Alloc(A CtorArg);
with A = void is illegal.

>What is happening and how can i correct this ? Also, is there a
>mechanism to pass any number of arguments to the ctors. That is
>flexibility in the number of arguments.


There isn't a convenient way to allow passing of any number of
arguments, but such a thing is being considered:

http://std.dkuug.dk/jtc1/sc22/wg21/d...2003/n1483.pdf

The syntax proposed there doesn't seem to cater to placement new calls
though.

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function template type deduction from default parameter tobias.loew@steag.com C++ 2 01-08-2013 07:53 AM
decltype as a template parameter when containing reference to anothertemplate parameter. Isti C++ 2 04-19-2010 10:01 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM



Advertisments