Hi, Daniel.
This is your example:
template <class T>
void S::f()
{
šif(g is specialized) // (1)
š šcallbacks_ready=true;
š// else wait for a callback object to be registered
}
At (1) the runtime detection is used, but "S:g()" was specialized at compile time.
Templates are specialized and compiled at compile time, but not runtime.
We can't write "if(g is specialized)" - it is impossible to realize it at runtime (and it is senseless).
So let me to recommend you some books of H. Sutter and A. Alexandrescu.
There are a lot of recommendations about templates, their specializations, overloads, static/dynamic polymorphisms and more.
1st book:
[http://www.gotw.ca/publications/xc++s.htm]
Pay attention to this chapter of the book:
"Item 7: Why Not Specialize Function Templates?"
2nd book;
"C++ Coding Standards: 101 Rules, Guidelines, and Best Practices" (authors H. Sutter and A. Alexandrescu)
Pay attention to 64, 65, 66 and 67 chapters.
I hope you will find some useful ideas about templates in these books.
There are also Boost libraries targeted to realize static polymorphism:
1) http://www.boost.org/doc/libs/1_40_0?view=category_Generic
2) http://www.boost.org/doc/libs/1_40_0?view=category_Metaprogramming
Best,
Andrew.
> Slightly OT, but I was hoping someone here might know the magic incantation
>
(which may involve a Boost library).
> Given a template class such as
> template <class T>
>
struct S
>
{
>
šš void f();
>
šš void g() { T t(h()); cb(t) }
>
šš T & h();
>
šš boost::function1<void, T &> cb;
>
};
> Is there a way to have S.f() behave differently depending on whether S.g() uses
>
the default definition or whether it is specialized for this particular type?
>
This detection needs to occur before S.g() is ever called.
> For example, the mere existance of the specialization
>
template <> void S<int>::g() { ... }
>
should trigger special logic in S<int>::f().
> Thanks,
>
Daniel
> P.S. The motivation for this is an attempt to retrofit a callback API that
>
allows specialization of S.g() or the registration of a boost::function that
>
gets called by the default S.g(). We need to disable callbacks until an actual
>
function is registered via either method. It is easy to dispatch on
>
registration of the boost::function; I don't know how to detect the
>
specialization.