Re: what is wrong with this code?
 
On 23 Jul., 08:02, terry wrote:
SG wrote:
I expect a good C++0x library implementation to use the "small
function optimization" in std::function so that there won't be heap
allocations for small function objects [2]. Still, the arguments are
forwarded *twice* within std::function which also includes a virtual
function call.
I agree completely, this is why I raised the question.
For virtual function calls where the pointer has to be resolved (to the
correct class and member function) before the binding can take place
there is a real advantage in allowing this binding to happen early and
providing a "pointer of class-agnostic type" you suggest.
Here's another thought: A compiler & library vendor could support this
"pointer of class-agnostic type" (delegate) internally and use it
inside std::bind as an optimization:
   class A {
   protected:
     ~A() {}
   public:
     virtual foo() = 0;
   };
   class B : public A {
   public:
     void foo();
   };
   void g() {
     B b;
     A* pa = &b;
     auto f = std::bind(&A::foo,pa);
     f();
   }
I think the specification of std::bind allows an implementation to use
a "delegate" type internally if the compiler supports it as extension.
So, a quality implementation with compiler support for delegates could
make the result of std::bind(&A::foo,pa) class-type-agnostic by using
this special delegate type. The will reduce code bloat and also allow
"early binding" (no virtual lookup needed when f is invoked).
Cheers!
SG
-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]