I want to store a function, or a reference thereto, in a class as member variable. I am aware of both storing a function pointer as well as std::function or boosts equivalent. However, I recently read that using pointers results in a call overhead compared to template parameters. In that search I stumbled upon this post, but I fail to reproduce working code from the answer. Here is what I tried, following some of the answers:
template<typename ftype>
class MyClass
{
private:
ftype func;
public:
MyClass(ftype func_)
:func(func_)
{}
};
double dosth(double x)
{
return x*x;
}
int main()
{
MyClass<decltype(dosth)> entity(dosth);
}
However, it results in the error:
error: data member instantiated with function type 'double (double)'
note: in instantiation of template class 'MyClass< double (double)>' requested here
I'm not sure where the exact problem lies, but I would like the exact function type to be inputted at compile time.
So my question is: Writing a class that somehow needs the information of a function that will repeatedly be called (and only called), what can I do to avoid said overhead?