Remove duplicate code from CGFunctions by using C++ templates.