Damian
Damian

Reputation: 5561

How to make a class template argument optional?

Is there any way to make a template class argument optional?
Specifically in this example:

template <typename EVT>
class Event : public EventBase {
public:             
    void raise(EVT data){
        someFunctionCall(data);
    }
}

I want to have a version of the same template equivalent to this:

class Event : public EventBase {
public:             
    void raise(){
        someFunctionCall();
    }
}

But I don't want to duplicate all the code. Is it possible?

Upvotes: 1

Views: 619

Answers (1)

BЈовић
BЈовић

Reputation: 64223

With default template argument, and template specialization :

template <typename EVT=void>
class Event : public EventBase {
public:             
    void raise(EVT data){
        someFunctionCall(data);
    }
};

template <>
class Event<void> : public EventBase {
public:             
    void raise(){
        someFunctionCall();
    }
};

However, I don't see how would the EventBase look like.

Upvotes: 3

Related Questions