Default type-value for template parameter type

R

Ragnar

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
 
R

Renato Battaglia

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.
 
T

tom_usenet

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 said:
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/docs/papers/2003/n1483.pdf

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

Tom
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top