# Include <stdio. h>
// Template, which can call functions or functor
Template <typename functor>
Void testtemplate (functor func)
...{
Func ("testtemplate ");
}
// C function Style
Void testfunction (const char * Str)
...{
Printf ("testfunction: % s", STR );
}
// Functor Style
Struct testfunctor
...{
Void operator () (const char * Str)
...{
Printf ("testfunctor: % s", STR );
}
Testfunctor ()
...{
Printf ("testfunctor ()");
}
~ Testfunctor ()
...{
Printf ("~ Testfunctor ()");
}
};
Int main ()
...{
Testtemplate (testfunction); // function pointer as the call Parameter
Printf ("========================================== ===== ");
Testtemplate (testfunctor (); // The temporary functor object is used as the call parameter.
Printf ("========================================== ===== ");
Testfunctor OBJ; // generate a functor instance
Testtemplate <testfunctor &> (OBJ); // call with reference type to avoid copying
Return 1;
}