You have two options. Pass the function at runtime:
#include <functional> template <typename ComplexBinaryOp> ComplicatedObject BinaryOp(const ComplicatedObject& B, ComplexBinaryOp op) const { // ... Cf[i] = op(f[i], Bf[i]); // ... } // functor wrapping member function pointer BinaryOp(B, std::mem_fn(&std::complex<double>::operator+)); // standard-issue functor BinaryOp(B, std::plus<std::complex<double>>());
Or pass it at compile time:
// or another floating-point type typedef double (*ComplexBinaryOp)(double, double); template <ComplexBinaryOp op> ComplicatedObject BinaryOp(const ComplicatedObject& B) const { // ... Cf[i] = op(f[i], Bf[i]); // ... } // non-member function template<class T> std::complex<T> add_complex(const std::complex<T>& a, const std::complex<T>& b) { return a + b; } // non-member function pointer BinaryOp<add_complex<double>>(B);
I believe that you can do the same with member function pointers by changing the definition of ComplexBinaryOp .
Jon purdy
source share