TITLE: getting around temporary function template limitation Allan D. Clarke (clarke@ses.com): The current definition of the C++ language allow function templates like the following: template void foobar () { ... } Many of the current compilers have not yet caught up with the standard and refuse to compile this code. They require that any template parameters also be formal arguments to the function. Thus this is legal template void foobar (T* t, U* u) { ... } As a side note, the syntax for calling this function looks like below (it resembles the syntax for the new cast operators). foobar (parm1, parm2); Here is a workaround until compilers meet the standard. The trick relies on two things: - class templates don't have this restriction - nested static functions have access to the class template arguments. Here is the workaround code template class foobar { public: static void doit () { ... } }; Here is a concrete example. Suppose that we have a graph editing application. We would like to declare a suite of functions which constrain allowable connections between different nodes based upon their class, as in template bool allow (Link* l) { return dynamic_cast(l->srcNode()) && dynamic_cast(l->dstNode()); } This would become instead template class Allow { public: static bool ok (Link* l) { return dynamic_cast(l->srcNode()) && dynamic_cast(l->dstNode()); } }; An example client-side code might be typdef bool (*pfb) (Link* l); ... pfb Allowed [10]; Allowed[0] = Allow::ok; ... Allowed[9] = Allow::ok; ... Link* l = ...; for (int i = 0; i< 10; i++) { if ((Allowed[i])(l)) ... } Thanks to Jamshid Afshar for pointing out this technique.