开发者

Dynamic creating of typedef

开发者 https://www.devze.com 2023-01-04 04:25 出处:网络
I\'m creating event system. It\'s based under boost::signals. To make the work easier I\'m using typedef for the function signatures.

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 po开发者_如何学Pythonssible 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.


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


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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消