virtual template members

A

Amadeus W. M.

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()(const Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

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

private:

};

Similarly other Transform derived classes.
 
V

Victor Bazarov

Amadeus said:
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()(const Vector_t & x, Vector_t & y) const {};

protected:

};

class Affine : public Transform
{
public:

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

private:

};

Similarly other Transform derived classes.

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
 
A

Amadeus W. M.

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

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(Source, 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).
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top