mixing virtual functions and template functions - how?

Nov 11, 2009 at 7:23am
while syntactically templates cannot be virtual functions, I wonder what approach to use when semantically this is what someone wants to do. for example, in my case I want to have a virtual function, because I'm using sub-classes while having a pointer to the base class, but I also want to use template functions, as the function I'm declaring has a parameter that I only know the concept of (iterator), but not the exact type.

here's the code that I'd want to accomplish from a semantic perspective, but which is not possible as it would include virtual function templates. I wonder what approach to use to achieve a similar result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <vector>

// some base class A
class A {
public:
    A() {}
    virtual ~A() {}

    template<typename iterator>
    virtual void ize(iterator it) = 0;
};

// some derived class B
class B : public A {
public:
    int b;

    B() { b = 2; }
    virtual ~B() {}

    template<typename iterator>
    virtual void ize(iterator it) {
        *it = b;
        ++it;
    }
};

// some derived class C
class C : public A { ... };

// some factory method
A * factory(int type) {
    switch (type) {
        case 1: return new B();
        case 2: return new C();
          ...
        default: return 0;
    }
}


int main() {
    int type = 1;

    // get a subclass based on type
    A *a = factory(type);

    // use a with a vector
    std::vector<int> v;
    a->ize(std::back_inserter(v));

    // use a with an array
    int d[1];
    a->ize(d);

    delete a;

    return 0;
}

Last edited on Nov 11, 2009 at 11:25am
Nov 11, 2009 at 10:21am
You do have a contadiction. If you think about how virtual functions are implemented, you'll realise that you don't really want this.

Are you familiar with the implementation of virtual functions and the use and role of the vtable?

Couple that with templates generating new code on the fly.
Nov 11, 2009 at 11:32am
kbw, yes, I do have an understanding on how virtual functions are implemented.

and yes, the two concepts really go against each other. though one could force the compiler generate code for _all_ known virtual functions for each new template invocations encountered, instead of code generation on the fly :) this would of course add functions to the vtable of all of these classes.

but, if not so - how can one solve this issue? write a virtual function that can accept a concept as a parameter, instead of a specific class?
Nov 11, 2009 at 1:27pm
There is no easy solution.

Just off the top of my head, and knowing nothing about what you are trying to do, you might be able to use boost::variant<> coupled with boost::static_visitor<>. Or make everything a template.

[/code]
template < typename T, typename Iterator >
void ize( T& t, Iterator i )
{
t.ize( i );
}
[/code]

To use either approach, you would no longer use inheritance or virtual functions.
Topic archived. No new replies allowed.