[PATCH] Optimize pointers to member function

John McCall rjmccall at apple.com
Thu Feb 14 13:29:29 PST 2013


On Feb 14, 2013, at 12:20 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> On Thu, Feb 14, 2013 at 11:58 AM, Olivier Goffart <ogoffart at kde.org> wrote:
> On Thursday 14 February 2013 11:05:28 John McCall wrote:
> > On Feb 14, 2013, at 3:57 AM, Olivier Goffart <ogoffart at kde.org> wrote:
> > > This patch optimize comparison and calling of pointers to member function.
> > >
> > > If we know that a given class do not have multiple inheritance, we can
> > > simplify the comparison and the calls as the pointer to this will never be
> > > shifted.
> > > On the Microsoft ABI (which is not implemented in clang), the size of a
> > > pointer to a member function is even reduced in that case.[1]
> >
> > A pointer-to-member may point to a member of a derived class.
> 
> How?
> 
> struct A { void m(); };
> struct B : A { void n(); };
> int main() { void (A::*t)() = &B::n; }
> 
> error: cannot initialize a variable of type 'void (A::*)()' with an rvalue of
> type 'void (B::*)()': different classes ('A' vs 'B')
> 
> 
> C++11[4.11.2]
>    A prvalue of type “pointer to member of B of type cv T”, where B is a class
>    type, can be converted to a prvalue of type “pointer to member of D of type
>    cv T”, where D is a derived class (Clause 10) of B. [...]
> 
> You can convert from a base to a derived, but not the other way around.
> 
> [expr.static.cast]p12.
> 
> int main() { void (A::*t)() = (void(A::*)())&B::n; }

Right.  You can also reinterpret_cast a pointer to member to a completely
unrelated pointer to member type, as long as they agree about function vs.
non-function.  It's not undefined behavior until you try to use a pointer to
member on an object whose dynamic type does not have that member.

> Notice that since Microsoft changes the size of the pointer depending on that
> fact, it means it cannot be possible to point to a member of a derived class.
> 
> No, it means Microsoft's implementation is non-conforming. See MS compiler warning C4407.

Precisely.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130214/c236b858/attachment.html>


More information about the cfe-commits mailing list