inline empty polymorphic functions
Suppose condition is true. Can I ever expect a compiler to omit the bolded virtual function call?
Code:
#include <iostream>
namespace scratch
{
class foo
{
public:
foo(method & m_) : m(m_) {}
void operator()()
{
std::cout << "Functor called." << std::endl;
m->call();
}
private:
method & m;
};
struct method { virtual void call() = 0; };
class trivial : public method
{
public:
void call() {}
};
class nontrivial : public method
{
public:
void call()
{
//Juicy with code
}
};
}//namespace scratch
int main()
{
using namespace scratch;
method * m = (condition) ? new trivial: new nontrivial;
foo functor(*m);
functor();
delete m;
}
As was suggested on a previous thread, I'm going to implement a customize-your-own-function sort of class by having the operator() call a bunch of function objects from reference, a few of which might have been chosen to do nothing. What is the overhead involved in calling a virtual function that does nothing? How can I find out in the future to avoid asking such questions? What is the answer to life, the universe, and the meaning of everything?
*edit* I can probably spare a few cycles per call, but any more will add up quickly. The operator() will have a tremendous loop inside of it.