Created
July 6, 2016 11:13
-
-
Save bfueldner/4df6b114a984846af81cb349128ba51d to your computer and use it in GitHub Desktop.
Template with functor parameter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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