Skip to content

Instantly share code, notes, and snippets.

@tilgovi
Created November 6, 2011 02:30
Show Gist options
  • Save tilgovi/1342375 to your computer and use it in GitHub Desktop.
Save tilgovi/1342375 to your computer and use it in GitHub Desktop.
Function building with templates and overloading
#include <iostream>
template <typename Ret, typename Arg>
class Prod {
private:
Ret (*left)(Arg);
Ret (*right)(Arg);
public:
Prod(Ret (*l)(Arg), Ret (*r)(Arg)) {
this->left = l;
this->right = r;
}
Ret operator()(Arg a) {
return this->left(a)*this->right(a);
}
};
double div3(double a) {
return a/3.0;
}
double div2(double a) {
return a/2.0;
}
int main(int argc, char *argv[]) {
Prod<double, double> p = Prod<double, double>(div3, div2);
std::cout << p(9) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment