开发者

How to make a class template argument optional?

开发者 https://www.devze.com 2023-04-06 16:10 出处:网络
Is there any way to make a template class argument optional? Specifically in this example: template <typename EVT>

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 ver开发者_开发知识库sion 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?


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.

0

精彩评论

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