3

I'm creating event system. It's based under boost::signals. To make the work easier I'm using typedef for the function signatures.

Everything is okey until I need creating of some new event trought event's system method. I have to create the typedef dynamically on given type to the template function. The problem is the name of typedef.

Some pseudocode I would have:

template<typename EventType>
void create(const string &signalName)
{
   typedef EventType signalName;
   // ...
}

Is it possible to make such typedef (their names) with some passed string or data or something else? I don't want to make user care about of this.


UPD: So, I have some typedefs list of function signatures - events. I have some templated function for connecting slots, for example. I don't want to force user to input signature by his hands again every time (user is programmer which will use the event system). So I just use my typedefs from special namespace into template argument.

Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • 3
    Suppose such a thing were possible. How do you envision *using* that typedef once you've created it? Telling your intended use might help people think of alternative solutions. – Rob Kennedy Jun 18 '10 at 22:38
  • 1
    Thank you, but that didn't really answer my question. I meant that you should demonstrate how you would call that `create` function, and then demonstrate how you would use the resultant typedef. Show some example code for how you would use such a feature. – Rob Kennedy Jun 18 '10 at 22:56

3 Answers3

4

typedefs only matter during compilation. As far as I know it's just an alias for a type.

Skurmedel
  • 21,515
  • 5
  • 53
  • 66
3

Template parameters are, by definition, compile time entities. You can not dynamically create template classes on the fly during program execution as far as I am aware.

wheaties
  • 35,646
  • 15
  • 94
  • 131
1

In this case, I wouldn't go for typedef's. If you want to have several types of events and create them dynamically, you can use a simple class containing the information about the event type. When you have an event, you link it to the event type created before.

Something like this:

class EventType
{
  private:
    string type;

  EventType(string type);
};

class Event
{
  private:
    string event_name;
    EventType *event_type;

  Event(string event_name, EventType event_type);
};

...

void create(const string &signalName)
{
  EventType *event_type = new EventType("type_x");
  Event *event = new Event("event_x", event_type);
}
Juliano
  • 821
  • 6
  • 21