Skip to content

Instantly share code, notes, and snippets.

@bfueldner
Created July 6, 2016 11:13
Show Gist options
  • Save bfueldner/4df6b114a984846af81cb349128ba51d to your computer and use it in GitHub Desktop.
Save bfueldner/4df6b114a984846af81cb349128ba51d to your computer and use it in GitHub Desktop.
Template with functor parameter.
struct linear
{
int operator()(const int value) const
{
return value;
}
};
struct logarithmic
{
int operator()(const int value) const
{
return (value * value) >> 8;
}
};
template<typename T = linear>
struct calculator
{
T m_func;
int call(const int value)
{
return m_func(value);
}
};
int main(int, char **)
{
calculator<linear> flin;
std::cout << flin.call(100) << std::endl;
calculator<logarithmic> flog;
std::cout << flog.call(100) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment