Partial template specialization

Discussion in 'C++' started by Hicham Mouline, Sep 24, 2007.

  1. Hi

    I have a class template Foo with 10 non-static member methods.
    Foo needs to be partially specialized because the implementation of some
    (perhaps 2 or 3) of the methods in
    the primary template is not appropriate for the specialized template.

    I made the partially specialized template inherit from the primary template
    in order to use the primary implementations
    for the 7 methods and the specialized implementations for the other 3
    methods.
    All the methods of the primary template, that is the base class, are now
    non-pure virtual.

    Other ways to do this?

    rds,
     
    Hicham Mouline, Sep 24, 2007
    #1
    1. Advertising

  2. Hicham Mouline

    werasm Guest

    Hicham Mouline wrote:
    > Hi
    >
    > I have a class template Foo with 10 non-static member methods.
    > Foo needs to be partially specialized because the implementation of some
    > (perhaps 2 or 3) of the methods in
    > the primary template is not appropriate for the specialized template.


    If they are not appropriate, how could you inherit from the primary
    template? Also, if you inherit from the primary template from a
    specialization, obviously you need to change your template params,
    right, otherwise you instantiate the template recursively.

    Perhaps post your code, or small example.

    Werner
     
    werasm, Sep 24, 2007
    #2
    1. Advertising

  3. "werasm" <> wrote in message
    news:...
    >
    > Hicham Mouline wrote:
    >> Hi
    >>
    >> I have a class template Foo with 10 non-static member methods.
    >> Foo needs to be partially specialized because the implementation of some
    >> (perhaps 2 or 3) of the methods in
    >> the primary template is not appropriate for the specialized template.

    >
    > If they are not appropriate, how could you inherit from the primary
    > template? Also, if you inherit from the primary template from a
    > specialization, obviously you need to change your template params,
    > right, otherwise you instantiate the template recursively.
    >
    > Perhaps post your code, or small example.
    >
    > Werner
    >

    I was wrong.
    Basically this is what i need:

    template <typename A, const int n>
    class Foo {
    f1() .... f10();
    };

    // Partial specialization for n=2
    // just f2() and f3() from the primary template are not good for the case
    n=2,
    // so i need to "override" them, but only those 2
    // i wish not to redefine the other methods
    template <typename A>
    class Foo<A,2> : public Foo {
    f1() .... f10();
    };
     
    Hicham Mouline, Sep 24, 2007
    #3
  4. Hicham Mouline

    werasm Guest

    Hicham Mouline wrote:

    >
    > I was wrong.
    > Basically this is what i need:
    >
    > template <typename A, const int n>
    > class Foo {
    > f1() .... f10();
    > };
    >
    > // Partial specialization for n=2
    > // just f2() and f3() from the primary template are not good for the case
    > n=2,
    > // so i need to "override" them, but only those 2
    > // i wish not to redefine the other methods
    > template <typename A>
    > class Foo<A,2> : public Foo {
    > f1() .... f10();
    > };



    A quick structure that comes to mind (although I haven't given this
    that much thought) is:

    1) Create a base class per function, and specialize where required.

    2) Create a common data component that represents your private
    data. This you could store as reference in each of your bases, the
    real data living in the derived (privately). Alternatively you could
    use a virtual
    base for this purpose. I prefer the reference though, but it may not
    be touched during destruction (which makes sense as it may only
    be touched in the function that it represents, right?

    3) Derive from each of the bases. If N=2, for the cases where it
    matters
    the specialized base would automatically become the actual base. All
    the bases form part of the interface, and the Derived (Concrete)
    basically
    consist of a constructor and private data only. Get it?

    Regards,

    Werner
     
    werasm, Sep 24, 2007
    #4
  5. Hicham Mouline

    werasm Guest

    Hicham Mouline wrote:


    > I was wrong.
    > Basically this is what i need:

    [snip]
    I hope I understood you correct. If so, this
    is what I had in mind. This allows you to
    specialize, whereupon the method is replaced
    by the specialized. There are other ways
    in which you could share data as well. Boost's
    base_from_member idiom comes to mind.

    Here goes (It does not link as you have
    to implement do1() and do2()!):

    template <class T>
    struct FooData
    {
    public:
    FooData( T val )
    : value_(val){ }
    protected:
    FooData(){ }
    T value_;
    };

    template <class T, int N>
    struct FooFun1
    : virtual FooData<T>{ void do1(); };

    template <class T, int N>
    struct FooFun2
    : virtual FooData<T>{ void do2(); };

    template <class T>
    struct FooFun2<T,2>
    : virtual public FooData<T>{ void do2(); };

    template <class T, int N>
    class Foo
    : public FooFun1<T,N>,
    public FooFun2<T,N>
    {
    public:
    Foo( T data )
    : FooData<T>( data ),
    value_( FooData<T>::value_ )
    { }
    private:
    //Hides base protected member.
    // Not really used.
    T& value_;
    };

    int main()
    {
    Foo<int,2> test( 20 );
    test.do1();
    test.do2();//Specialization called.
    return 0;
    }

    The hiding of the name value_ is not necessary,
    as it is not the intention of anybody to derive
    from Foo anyway. I just put it there for the
    bureaucrats ;-).

    Regards,

    Werner
     
    werasm, Sep 24, 2007
    #5
    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.
Similar Threads
  1. Chandra Shekhar Kumar

    template partial specialization

    Chandra Shekhar Kumar, Jun 24, 2003, in forum: C++
    Replies:
    17
    Views:
    3,055
    tom_usenet
    Jun 25, 2003
  2. Joseph Turian
    Replies:
    2
    Views:
    485
  3. toton
    Replies:
    1
    Views:
    627
  4. vj
    Replies:
    1
    Views:
    501
  5. Hizo
    Replies:
    17
    Views:
    711
    itaj sherman
    Mar 7, 2011
Loading...

Share This Page