Default type-value for template parameter type

Discussion in 'C++' started by Ragnar, Sep 3, 2003.

  1. Ragnar

    Ragnar Guest

    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
    Ragnar, Sep 3, 2003
    #1
    1. Advertising

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


    (Ragnar) wrote in message news:<>...
    > 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, Sep 3, 2003
    #2
    1. Advertising

  3. Ragnar

    tom_usenet Guest

    On 3 Sep 2003 03:29:23 -0700, (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/docs/papers/2003/n1483.pdf

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

    Tom
    tom_usenet, Sep 4, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page