Discussion:
Default type-value for template parameter type
(too old to reply)
Ragnar
2003-09-03 10:29:23 UTC
Permalink
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
Renato Battaglia
2003-09-03 14:01:13 UTC
Permalink
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.
Post by Ragnar
Hello
I have a template in my kernel slab allocator, used to created object
caches.
template<class T, class A = void>
class ObjectCache
{
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;
}
...
};
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.
ObjectCache<Lwp, KrThread *> LwpCache("LWPCache");
The second type is specified here as KrThread
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
tom_usenet
2003-09-04 09:05:36 UTC
Permalink
Post by Ragnar
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>
Post by Ragnar
class ObjectCache
{
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.
Post by Ragnar
...
};
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.
Post by Ragnar
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/docs/papers/2003/n1483.pdf

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

Tom

Loading...