views:

54

answers:

2

I currently have a class that uses template arguments. I need an array of these. How could I do this (without boost). ex:

template <typename RET, typename T>
class AguiTimedEvent {
    RET (*onEvent)(T arg);
    double timeStamp;
public:
    RET call(T arg);
    bool expired();
    AguiTimedEvent();
    AguiTimedEvent(RET (*Timefunc)(T arg), double timeSec);
};

and I would need to something like:

AguiTimedEvent t<int, int>(func, 5.0);
v.push_back(t);
...
v[0].call();

I don't actually need the return value, but I just have it to make it more flexible. If the return value causes an issue I could limit it to void functions, but defiantly the arg needs to be templated. What can I do? Thanks

*I need the vector to handle any type, I need an array, where the array can dispatch events of X Y, not just int int

+2  A: 
std::vector<AguiTimedEvent<int, int> > v;

If you need to store objects of different AguiTimedEvent types that have different template arguments, you need to create a base class (e.g., AguiTimedEventBase) and store pointers in the vector.

James McNellis
Note the extra space between "> >", otherwise your compiler won't read it right.
dutt
Grumble, +1 but I'm only boosting you higher on the top user list than me. >:(. I need to pass you. :)
GMan
@GMan: Ha ha. Well, good news for your position on the all-time list: now that C++ isn't really my most frequently used language anymore, I anticipate my participation in [c++] decreasing (just as soon as I stop banging my head on my desk wondering why C# can't be just a little more like C++ :-D).
James McNellis
I'm not sure how the base class idea would achieve this?
Milo
@Milo: It sounds like you are looking for something exactly like what [Boost.Function](http://www.boost.org/doc/libs/1_44_0/doc/html/function.html) provides (your standard library implementation may also provide `function`). Basically, you have the derived class type store the function pointer and you call that function through a virtual function declared in the base class.
James McNellis
@James McNellis so there is no way to do this using only the c++ 98 or c++ 03 standard?
Milo
@Milo: The Boost.Function library should work on any relatively standards-compliant C++ compiler (C++03).
James McNellis
@Milo: Boost isn't magic, it's still written in C++. You can open it up and attempt to re-write it yourself. I really suggest you adopt Boost, though.
GMan
@James McNellis I want to avoid using boost because it is a lot of file to drag around just for 1 functionality
Milo
@Milo: Looking at your recent questions, I'd say you'd benefit from using quite a few of the Boost libraries. As GMan said, it's all open source; you can easily download the Boost source and see how the polymorphic function wrappers are implemented (though, with all the compatibility support in Boost code, it may be quite an adventure). You can implement your own polymorphic function wrapper if you want: it's just a simple matter of programming.
James McNellis
A: 

I didn't face a problem with the following code:

template <typename RET, typename T>
class AguiTimedEvent {
    RET (*onEvent)(T arg);
    double timeStamp;
public:
    RET call(T arg) {return 0;}
    bool expired() {}
    AguiTimedEvent() {}
    AguiTimedEvent(RET (*Timefunc)(T arg), double timeSec) {}
};

int func(int x) {return 0;}

int main()
{
    vector< AguiTimedEvent<int, int> > v;
    AguiTimedEvent<int, int> t(func, 5.0);

    v.push_back(t);

    return 0;
}
Donotalo
But what if its not int int, i need the vector to handle any type
Milo
@Milo, do you mean a singe vector should contain multiple types? i don't think that's possible.
Donotalo
@Donotalo What would be a way to achieve the same effect?
Milo
@Milo, others are giving the solution. using base class - derived class etc.
Donotalo
@Donotalo Polymorphism is not really that useful for this since a derived class has to be created for each arg type
Milo
@Milo: That's exactly why polymorphism _is_ useful: you have many derived classes that all have the same behavior.
James McNellis
yea but a derived class for each argument is quite a bit, it feels like templates should be able to help for this, ex, creates the derived class at runtime based on template args
Milo
@Milo: C++ is statically typed; you can't create types at runtime.
James McNellis
@Milo: silly idea - use `void *`.
Donotalo