virtual template members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amadeus W. M.

    #1

    virtual template members

    Template member functions cannot be virtual - I know - but is there any
    way to simulate that (other than making the entire class templated,
    with non-templated members).


    What I have is this:

    class Transform
    {
    public:

    // Each derived Transform should compute y=Transform(x)
    // in its own way, so this operator() should be virtual,
    // but I also want it to be templated on the vector type.
    template <class Vector_t>
    void operator()(cons t Vector_t & x, Vector_t & y) const {};

    protected:

    };

    class Affine : public Transform
    {
    public:

    template <class Vector_t>
    void operator()(cons t Vector_t & x, Vector_t & y) const {
    // real calculation of y=Affine(x).
    }

    private:

    };

    Similarly other Transform derived classes.

  • Victor Bazarov

    #2
    Re: virtual template members

    Amadeus W. M. wrote:[color=blue]
    > Template member functions cannot be virtual - I know - but is there
    > any way to simulate that (other than making the entire class
    > templated, with non-templated members).
    >
    >
    > What I have is this:
    >
    > class Transform
    > {
    > public:
    >
    > // Each derived Transform should compute y=Transform(x)
    > // in its own way, so this operator() should be virtual,
    > // but I also want it to be templated on the vector type.
    > template <class Vector_t>
    > void operator()(cons t Vector_t & x, Vector_t & y) const {};
    >
    > protected:
    >
    > };
    >
    > class Affine : public Transform
    > {
    > public:
    >
    > template <class Vector_t>
    > void operator()(cons t Vector_t & x, Vector_t & y) const {
    > // real calculation of y=Affine(x).
    > }
    >
    > private:
    >
    > };
    >
    > Similarly other Transform derived classes.[/color]

    Of course, I don't know for sure, but it seems that you're misusing
    templates. The whole idea behind the generic programming is that types
    and/or values for which the structures and/or algorithms are created
    are _totally_ unrelated in C++ sense. The only thing that unifies
    those types or values are their _traits_. For example, iterators can
    be incremented using ++. Numbers can be added or multiplied. And so
    on.

    In your case I am questioning the relationship you have established
    between "Transform" and "Affine" (and "other Transform derived classes")
    while totally disconnecting the "vector" type they are about to work on.

    If 'Affine' and other derived types operate on some vectors, are those
    really totally unrelated vectors? Why then the Transforms themselves
    are not templates? Could it be that you've not separated your system
    abstractions enough?

    V
    --
    Please remove capital As from my address when replying by mail


    Comment

    • Amadeus W. M.

      #3
      Re: virtual template members

      On Thu, 23 Feb 2006 21:47:43 -0500, Victor Bazarov wrote:
      [color=blue]
      > Amadeus W. M. wrote:[color=green]
      >> Template member functions cannot be virtual - I know - but is there
      >> any way to simulate that (other than making the entire class
      >> templated, with non-templated members).
      >>
      >>
      >> What I have is this:
      >>
      >> class Transform
      >> {
      >> public:
      >>
      >> // Each derived Transform should compute y=Transform(x)
      >> // in its own way, so this operator() should be virtual,
      >> // but I also want it to be templated on the vector type.
      >> template <class Vector_t>
      >> void operator()(cons t Vector_t & x, Vector_t & y) const {};
      >>
      >> protected:
      >>
      >> };
      >>
      >> class Affine : public Transform
      >> {
      >> public:
      >>
      >> template <class Vector_t>
      >> void operator()(cons t Vector_t & x, Vector_t & y) const {
      >> // real calculation of y=Affine(x).
      >> }
      >>
      >> private:
      >>
      >> };
      >>
      >> Similarly other Transform derived classes.[/color]
      >
      > Of course, I don't know for sure, but it seems that you're misusing
      > templates. The whole idea behind the generic programming is that types
      > and/or values for which the structures and/or algorithms are created
      > are _totally_ unrelated in C++ sense. The only thing that unifies
      > those types or values are their _traits_. For example, iterators can
      > be incremented using ++. Numbers can be added or multiplied. And so
      > on.
      >
      > In your case I am questioning the relationship you have established
      > between "Transform" and "Affine" (and "other Transform derived classes")
      > while totally disconnecting the "vector" type they are about to work on.
      >
      > If 'Affine' and other derived types operate on some vectors, are those
      > really totally unrelated vectors? Why then the Transforms themselves
      > are not templates? Could it be that you've not separated your system
      > abstractions enough?
      >
      > V[/color]

      Well, everything is part of a bigger picture, which I haven't described.
      What connects the base Transform and the derived concrete Affine,
      Similarity, etc. is a mathematical operation called registration. Given
      two vectors X and Y of the same size, find the best transform T (within
      one of the derived categories), which aligns X to Y (in the mean-squared
      sense), i. e. find the transform for which T(xi) ~ yi for all i.
      Here X=(xi) and Y=(yi) are complex vectors. The actual T is computed
      differently, depending on its type.

      Each derived class computes its own parameters within a

      virtual void registration(So urce, Target)

      member. Once it does, I want to be able to apply the operator()(X,Y) to
      vectors of possibly different types, other than the types for which the
      registration was done. I really need this. There may be a design flaw
      somewhere else, but the project this is part of is too big to re-design.

      So I think the registration does warrant a hierarchy, because I do need an
      abstract Transform class, but it would be good if operator() was templated
      (and virtual).


      Comment

      Working...