circumventing recursive definition wrt template specialization

Discussion in 'C++' started by SainTiss, Nov 4, 2003.

  1. SainTiss

    SainTiss Guest

    Hi,

    If you've got a template class with lots of methods,
    and then you've got a type which works with the template,
    except for one method...

    What you need to do there is specialize the template for your
    type. However, this requires you to copy the whole template, and
    change the method, which leads to code duplication...
    If there's only 1 template parameter, one can specialize the method
    instead of the whole class, but with more template parameters,
    this is impossible, because partial specialization of a
    method isn't allowed...

    Therefore, it would be handy to be able to do this:
    template<class A, class T> myClass<A,T*> : public myClass<A,T*>

    However, the compiler will complain that you're having a
    recursive definition, which is illegal...
    Obviously, the compiler thinks that myClass<A,T*> is being
    defined here, and therefore no other definition exists...
    But in this case, myClass<A,T*> is a template specialization,
    and therefore there IS another definition, namely the original
    template...

    So, is there a reason why this isn't possible, or is this just
    something that doesn't happen too often, and therefore not worth
    extending the standard?

    Thanks,

    Hans
    SainTiss, Nov 4, 2003
    #1
    1. Advertising

  2. SainTiss

    tom_usenet Guest

    On Tue, 04 Nov 2003 14:29:25 GMT, SainTiss <> wrote:

    >Hi,
    >
    >If you've got a template class with lots of methods,
    >and then you've got a type which works with the template,
    >except for one method...
    >
    >What you need to do there is specialize the template for your
    >type. However, this requires you to copy the whole template, and
    >change the method, which leads to code duplication...
    >If there's only 1 template parameter, one can specialize the method
    >instead of the whole class, but with more template parameters,
    >this is impossible, because partial specialization of a
    >method isn't allowed...
    >
    >Therefore, it would be handy to be able to do this:
    >template<class A, class T> myClass<A,T*> : public myClass<A,T*>
    >
    >However, the compiler will complain that you're having a
    >recursive definition, which is illegal...
    >Obviously, the compiler thinks that myClass<A,T*> is being
    >defined here, and therefore no other definition exists...
    >But in this case, myClass<A,T*> is a template specialization,
    >and therefore there IS another definition, namely the original
    >template...


    The problem is that specialization is couched in such terms as there
    *isn't* an original template. Often the original template wouldn't
    make sense or even compile for the type that you are specializing for.
    Also, how you you specify that you want to refer to a particular less
    specialized version? e.g.

    template <class U, class V, class W>
    class Foo
    {
    };

    template <class U, class V>
    class Foo<U, V, int>
    {
    };

    template <class U, class V>
    class Foo<U, int, V>
    {
    };

    template <class U>
    class Foo<U, int, int>: public Foo<U, int, int>//which specialization?
    {
    };

    I think it was considered a bit illogical, since a specialization is
    by definition the version of the template for the particular
    parameters - there is no "original" template, rather a "fall back"
    definition. E.g. often this is done:

    template <class T>
    class Foo; //never defined

    //specialisations are defined.

    >So, is there a reason why this isn't possible, or is this just
    >something that doesn't happen too often, and therefore not worth
    >extending the standard?


    There's a simple workaround - put the common stuff in a
    (implementation only, not interface) base class. To accomodate this
    feature, rather than extending the standard, large sections would have
    to be rewritten, since it contradicts what is said elsewhere. The
    definition as written is reasonably compact and logical, whereas what
    this extension would mean in syntax and consistency terms isn't.

    Tom
    tom_usenet, Nov 4, 2003
    #2
    1. Advertising

  3. SainTiss wrote in news:9JOpb.2857$-ops.be:

    > Hi,
    >
    > If you've got a template class with lots of methods,
    > and then you've got a type which works with the template,
    > except for one method...
    >
    > What you need to do there is specialize the template for your
    > type. However, this requires you to copy the whole template, and
    > change the method, which leads to code duplication...


    Here's another common way of doing it:

    #include <iostream>

    namespace detail
    {
    template < typename U, typename V >
    struct basic_method_helper;

    }

    template < typename U, typename V >
    struct basic
    {
    typename
    detail::basic_method_helper< U, V >::return_type
    method( V & v )
    {
    return detail::basic_method_helper< U, V >::apply( this, v );
    }
    V multiply;
    basic( V f ): multiply( f ) {}
    };

    namespace detail
    {
    template < typename U, typename V >
    struct basic_method_helper
    {
    // non-specialized versions
    typedef U return_type;
    static return_type apply( basic< U, V > *that, V & v )
    {
    return U(v * that->multiply);
    }
    };

    template < typename U >
    struct basic_method_helper< U, U >
    {
    // partialy specialized version
    typedef U return_type;
    static return_type apply( basic< U, U > *that, U & v )
    {
    return v * that->multiply;
    }
    };
    }

    int main()
    {
    double d = 2.5;

    basic< int, double > bid( 3.5 );
    basic< double, double > bdd ( 7 );

    std::cerr << bid.method( d ) << "\n";
    std::cerr << bdd.method( d ) << "\n";
    }


    Its a fair bit of typing, but more maintainable than copying a
    whole class.

    > If there's only 1 template parameter, one can specialize the method
    > instead of the whole class, but with more template parameters,
    > this is impossible, because partial specialization of a
    > method isn't allowed...
    >
    > Therefore, it would be handy to be able to do this:
    > template<class A, class T> myClass<A,T*> : public myClass<A,T*>
    >


    Well why not just allow partial specialization of a function's,
    member and non-member. I've read talk that a future standard
    might allow this, but wether this would extend to member function's
    I don't know (but I can't see why not).

    If you want to talk about the why's of the current standard and
    it's future direction's comp.std.c++ maybe better than here.

    [snip]

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Nov 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.
Similar Threads
  1. Amon Tse
    Replies:
    2
    Views:
    1,232
    John Carson
    Jun 11, 2005
  2. ma740988
    Replies:
    9
    Views:
    292
    ma740988
    Apr 11, 2006
  3. Joseph Turian
    Replies:
    2
    Views:
    451
  4. Pierre Yves
    Replies:
    2
    Views:
    468
    Pierre Yves
    Jan 10, 2008
  5. Zhang Guangyu
    Replies:
    2
    Views:
    837
Loading...

Share This Page