I want to define two kinds of member functions run
according to one template paramter of class. Here is my code:
template <int VAL, typename D = std::chrono::seconds, bool IS_DAILY = false>
class TimerJob {
public:
template <typename C, typename F, typename... Args, typename = std::enable_if_t<IS_DAILY>>
void run(F C::*f, C* c, Args&&... args) {
td_ = std::make_unique<std::thread>(
[](F C::*f, C* c, Args... args){
do {
c.f(args...);
std::this_thread::sleep_for(D{VAL});
} while (true);
});
}
template <typename C, typename F, typename... Args, typename = std::enable_if_t<!IS_DAILY>>
void run(F C::*f, C* c, Args&&... args) {
td_ = std::make_unique<std::thread>(
[](F C::*f, C* c, Args... args){
do {
c.f(args...);
// just an example to make a difference with the other run()
// the real case is much complicated
std::this_thread::sleep_for(D{VAL + 60});
} while (true);
});
}
private:
std::unique_ptr<std::thread> td_;
};
As you see, I'm trying to overload the function run
with different IS_DAILY
.
But I got an error:
<source>:27:8: error: 'template<int VAL, class D, bool IS_DAILY> template<class C, class F, class ... Args, class> void TimerJob<VAL, D, IS_DAILY>::run(F C::*, C*, Args&& ...)' cannot be overloaded with 'template<int VAL, class D, bool IS_DAILY> template<class C, class F, class ... Args, class> void TimerJob<VAL, D, IS_DAILY>::run(F C::*, C*, Args&& ...)'
27 | void run(F C::*f, C* c, Args&&... args) {
| ^~~
<source>:15:8: note: previous declaration 'template<int VAL, class D, bool IS_DAILY> template<class C, class F, class ... Args, class> void TimerJob<VAL, D, IS_DAILY>::run(F C::*, C*, Args&& ...)'
15 | void run(F C::*f, C* c, Args&&... args) {
I'm confused now... Isn't std::enable_if
used like this?
BTW, I'm working with C++14.