Re: Partial template class specialization?
On 31 Mar, 18:34, Victor Bazarov <v.baza...@comcast.invalid> wrote:
On 3/31/2011 11:26 AM, MikeWhy wrote:
"Qi" <n...@no.no> wrote in messagenews:imujtd$kpu$3@speranza.aioe.org..=
..
On 2011-3-30 1:18, MikeWhy wrote:
template <bool AutoReset, class T>
class TimerNode {
...
void OnTimer();
void DoCall();
};
//----------------------
template <class T>
void TimerNode<true, T>::OnTimer()
{
Reset();
DoCall();
}
//----------------------
template <class T>
void TimerNode<false, T>::OnTimer()
{ DoCall();
}
You can only partial specialize template.
You need to split TimerNode to two templates,
TimerNodeBase and TimerNode (inherited from TimerNodeBase).
Then put OnTimer in TimerNode and partial specialize TimerNode.
Does that actually change anything? I can separate the classes as you
say, but it still has the problem of partial specialization.
template <typename T> class TimerNodeBase {...};
template <bool AutoReset, typename T>
class TimerNode : public TimerNodeBase<T> {...}; // as before.
template <> void TimerNode<true>::OnTimer(){ ... } // problems, as befo=
re
template <> void TimerNode<false>::OnTimer(){ ... } // problems, as bef=
ore
The full context is as follows. TimerNode is embedded in class
CallbackTimer, templatized on its calllback client. They pre-existed
that way with auto-reset being the default and only behavior. I wanted
to add an option to just unlink from the timer and not auto-reset, henc=
e
the new
template <typename T, bool AutoReset = true>
class CallbackTimer
{
...
struct TimerNode; // as before.
private:
DoubleLinkedList<TimerNode> timers;
};
It would be nice to have that supported simply in the language. I
haven't checked the more recent language proposed standard.
Get a copy of the "Modern C++ Design" by Alexandrescu. What you seem t=
o
want to do is to make your resetting feature optional. Policy fits
right into that. Essentially you write
template<typename T, typename ResetPolicy>
class CallbackTimer
{
...
void OnTimer() {
ResetPolicy::Reset(this); // if you need anything
DoCall
}
};
struct ResetPolicyReset
{
template<class T> static void Reset(T* pCBTimer) {
pCBTimer->Reset();
}
};
struct ResetPolicyDontReset
{
static void Reset(void*) {} // do nothing
};
...
// when you need a timer that resets:
CallbackTimer<TickProc, ResetPolicyReset> resettingTime=
r;
// when you need a timer that doesn't reset:
CallbackTimer<TickProc, ResetPolicyDontReset> plainTimer;
Now, just like with the 'if (bReset)' that we discussed before, you have
to rely on the compiler to not generate code that is not needed.
Policy-based design (like type traits, too) rely on the compiler's
ability to refrain from generating code when it's not needed. Try it.
V
--
I do not respond to top-posted replies, please don't ask
Hi,
or you can try something like:
// code
#include <iostream>
template< int K >
struct CTypeFromInt
{};
template <bool AutoReset, class T>
class TimerNode {
public:
void Reset() { std::cout << __func__ << std::endl; };
void ConditionalAutoReset( CTypeFromInt< true > ) { Reset(); }
void ConditionalAutoReset( CTypeFromInt< false > ) { }
void OnTimer()
{ ConditionalAutoReset( CTypeFromInt< AutoReset >()); DoCall();}
void DoCall() {}
};
int main()
{
TimerNode< true, int > obj1;
obj1.OnTimer();
std::cout << "---\n";
TimerNode< false, int > obj2;
obj2.OnTimer();
}
// end code