Re: templates and virtual
On 21/01/10 10:39, Thomas Richter wrote:
Hi folks,
yes, templates and virtual member functions do not fit, I know. However,
I have here a situation where exactly that would be practical:
I have N worker classes that all provide the same interface, say:
class Interface {
public:
virtual double measure(const int *data1,const int *data2) = 0;
};
class A1 : public Interface {
// implements measure...
};
class A2 : public Interface {
// implements measure...
};
A clear application of virtual functions. Now, however, the same
function ("measure") is also required for a limited set of scalar types
that is known in advance. Say, "int", "short", "long" (doesn't matter
here). The implementation of "measure" is in all implementing interfaces
similar enough to share the code and generate it by a template.
Unfortunately, while it is know for which types I will need an
implementation for, the specific type required is not known at the type
an instance of the class is created, i.e. I cannot just template the
class itself by the type. It is known at call-time, but not at creation
time.
That is, I would prefer to write:
class Interface {
public:
template <typename T>
virtual double measure(const T *data1,const T* data2) = 0;
};
with classes implementing the interface requiring to implement the
template for all types T from a set of types specified in advance.
Clearly, the above is not possible in C++ (how would I tell the compiler
which T's are part of the interface specifications.
Which alternatives exist to generate a class hierarchy similar to the
above, i.e. require implementing classes of an interface to build
templates for a given set of types?
You can use boost::mpl to generate interfaces and implementations for a
set of types like this:
[max@truth test]$ cat test.cc
#include <stdio.h>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/inherit_linearly.hpp>
namespace m = boost::mpl;
typedef m::vector<short, int, long> Types;
template<class Base, class T>
struct InterfaceFunction : Base
{
using Base::measure; // unhide Base::measure
virtual double measure(const T*, const T*) = 0;
};
template<class T>
struct InterfaceFunction<m::empty_base, T>
{
// m::empty_base does not have measure()
virtual double measure(const T*, const T*) = 0;
};
// generate an interface with one measure() overload for each of the Types
typedef m::inherit_linearly<Types, InterfaceFunction<m::_1, m::_2>
::type Interface;
// a function that invokes different overloads of Interface::measure
void fun(Interface& itf)
{
short a1 = 1, b1 = 2;
itf.measure(&a1, &b1);
int a2 = 3, b2 = 4;
itf.measure(&a2, &b2);
long a3 = 3, b3 = 4;
itf.measure(&a3, &b3);
}
// these implement Inteface::measure() by forwarding it to
Derived::doMeasure()
template<class Derived, class Base, class T>
struct InterfaceFunctionImp : Base
{
using Base::measure; // unhide Base::measure
double measure(const T* t1, const T* t2)
{
return static_cast<Derived*>(this)->doMeasure(t1, t2);
}
};
template<class Derived, class T>
struct InterfaceFunctionImp<Derived, m::empty_base, T> : Interface
{
// m::empty_base does not have measure()
double measure(const T* t1, const T* t2)
{
return static_cast<Derived*>(this)->doMeasure(t1, t2);
}
};
struct A1 : m::inherit_linearly<Types, InterfaceFunctionImp<A1, m::_1,
m::_2> >::type
{
template<class T>
double doMeasure(T const* t1, T const* t2)
{
return printf("%s\n", __PRETTY_FUNCTION__);
}
};
struct A2 : m::inherit_linearly<Types, InterfaceFunctionImp<A2, m::_1,
m::_2> >::type
{
template<class T>
double doMeasure(T const* t1, T const* t2)
{
return printf("%s\n", __PRETTY_FUNCTION__);
}
};
int main(int, char** argv)
{
A1 a1;
fun(a1);
A2 a2;
fun(a2);
}
[max@truth test]$ g++ -Wall -o test test.cc
[max@truth test]$ ./test
double A1::doMeasure(const T*, const T*) [with T = short int]
double A1::doMeasure(const T*, const T*) [with T = int]
double A1::doMeasure(const T*, const T*) [with T = long int]
double A2::doMeasure(const T*, const T*) [with T = short int]
double A2::doMeasure(const T*, const T*) [with T = int]
double A2::doMeasure(const T*, const T*) [with T = long int]
--
Max
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]