Nick, do you know for a fact that a C++ complier will optimize for the base case of a virtual function? I was under the impression that it doesn't know (as in can't determine at compile time) whether the function was overwritten or not so it doesn't favor any of the cases. In fact I can't even figure how it would if it wanted to optimize an indirect function call. I'm not trying to start a war, just to clarify my assumption. As it is I generally write code using virtual functions that I most often do overwrite. If what you say is true, then I am incurring the penalty most of the time and that would be bad...

Cheers,
Adrian


Nick Apperson wrote:
sure thing:

----------------------------------------------

struct foo {
  // some stuff gets executed
  virtual void do_something(somestruct &s); // base case executed by default
};

struct foo_foo : foo {
  // some stuff gets executed
};

struct foo_bar : foo {
  // some stuff gets executed
  virtual void do_something(somestruct &s); // special case
};

So then if we have say:

foo *f;
somestruct s;

// ... some code that sets f to something and changes s

f->do_something(s);

----------------------------------------

So now when the compiler is optimizing the code, it will optimize for the base case of foo::do_something by default unless it has information that is even better. If our code instead read with f as a function pointer, the compiler (assuming it can't determine the value) won't have a base case.
_______________________________________________
computer-go mailing list
computer-go@computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/

Reply via email to