On 6 mai, 14:56, DerTop...@web.de wrote:
> Michael Doubez schrieb:
> [snipped original problem about the problem of combining virtual
> methods with templates]
>
>
>
> > On of this approach is to use dynamic polymorphism, that is if you can
> > define a same contract on your type. As an example if your foo is only
> > displaying a message with the data in parameter, your contract if that
> > the type should support (ostream<<T), you define the interface:
>
> > struct foo_ostreamable
> > {
> > virtual ostream& output(ostream&)const=0;
> > };
>
> > Then you define a templated foo_streambable implementation:
>
> > template<typename T>
> > struct foo_ostreamable_imp: foo_ostreamable
> > {
> > T value;
> > foo_ostreamable_imp(const T& t):value(t){}
>
> > virtual ostream& output(ostream& os)const{return os<<value;}
> > };
>
> > And finally the parameter of SuperClass::foo with templated
> > constructor:
>
> > struct foo_param
> > {
> > template<typename T>
> > foo_param(const T& t){data.reset(new foo_ostreamable_imp<T>(t));
>
> > scoped_ptr<foo_ostreamable> data;
> > };
>
> > // foo_param can be written into ostream
> > ostream& operator<<(ostream& os,const foo_param& p)
> > {
> > return p.data->output(os);
> > }
>
> > And finally, you define your classes:
> > class SuperClass
> > {
> > public:
> > virtual void foo(const foo_param&) const = 0;
> > };
>
> > class SubClass: public SuperClass
> > {
> > public:
> > virtual void foo(const foo_param& p) const { cout << p <<
> > endl;}
> > } ;
>
> > The compiler will automatically resolve with correct subtype:
> > SubClass s;
> > s.foo(1);
> > s.foo(0.1);
> > s.foo("bar");
> > ...
>
> > If you want to keep/compare foo_param values, there is some management
> > to do such as deep copy and others to put it into canonical form. If a
> > type doesn't react as you want, you overload foo_param constructor.
>
> > There is an overhead but that depends on what you want.
>
> > --
> > Michael
>
> That's a good explanation. Is this pattern named already?
I think it is type erasure. It can be useful if you want virtual
member function that takes any iterator of a given kind (let say an
output iterator, it is easier to implement

) but are not too focused
on performances (an indirection plus a virtual call per call, an
allocation per copy , dynamic cast for comparison ...).
The idea is taken from an article in Overload published by ACCU:
http://accu.org/index.php/overloadonline
I don't remember which issue (around end of year 2007 IIRC)
--
Michael