[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-commits
Subject:    Re: [PATCH] Optimize pointers to member function
From:       John McCall <rjmccall () apple ! com>
Date:       2013-02-14 21:29:29
Message-ID: E3C46974-B0C9-4A17-A40E-A7907C976D0E () apple ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Feb 14, 2013, at 12:20 PM, Richard Smith <richard@metafoo.co.uk> wrote:
> On Thu, Feb 14, 2013 at 11:58 AM, Olivier Goffart <ogoffart@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@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.
[Attachment #5 (text/html)]

<html><head><meta http-equiv="Content-Type" content="text/html \
charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: \
space; -webkit-line-break: after-white-space; "><div><div>On Feb 14, 2013, at 12:20 \
PM, Richard Smith &lt;<a \
href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>&gt; \
wrote:</div><blockquote type="cite">On Thu, Feb 14, 2013 at 11:58 AM, Olivier Goffart \
<span dir="ltr">&lt;<a href="mailto:ogoffart@kde.org" \
target="_blank">ogoffart@kde.org</a>&gt;</span> wrote:<br><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class="im">On Thursday 14 \
February 2013 11:05:28 John McCall wrote:<br> &gt; On Feb 14, 2013, at 3:57 AM, \
Olivier Goffart &lt;<a href="mailto:ogoffart@kde.org">ogoffart@kde.org</a>&gt; \
wrote:<br> &gt; &gt; This patch optimize comparison and calling of pointers to member \
function.<br> &gt; &gt;<br>
&gt; &gt; If we know that a given class do not have multiple inheritance, we can<br>
&gt; &gt; simplify the comparison and the calls as the pointer to this will never \
be<br> &gt; &gt; shifted.<br>
&gt; &gt; On the Microsoft ABI (which is not implemented in clang), the size of a<br>
&gt; &gt; pointer to a member function is even reduced in that case.[1]<br>
&gt;<br>
&gt; A pointer-to-member may point to a member of a derived class.<br>
<br>
</div>How?<br>
<br>
struct A { void m(); };<br>
struct B : A { void n(); };<br>
int main() { void (A::*t)() = &amp;B::n; }<br>
<br>
error: cannot initialize a variable of type 'void (A::*)()' with an rvalue of<br>
type 'void (B::*)()': different classes ('A' vs 'B')<br>
<br>
<br>
C++11[4.11.2]<br>
&nbsp; &nbsp;A prvalue of type “pointer to member of B of type cv T”, where B is a \
class<br> &nbsp; &nbsp;type, can be converted to a prvalue of type “pointer to member \
of D of type<br> &nbsp; &nbsp;cv T”, where D is a derived class (Clause 10) of B. \
[...]<br> <br>
You can convert from a base to a derived, but not the other way \
around.<br></blockquote><div><br></div><div>[expr.static.cast]p12.</div><div><br></div><div>int \
main() { void (A::*t)() = (void(A::*)())&amp;B::n; \
}</div></div></blockquote><div><br></div>Right. &nbsp;You can also reinterpret_cast a \
pointer to member to a&nbsp;completely</div><div>unrelated pointer to member type, as \
long as they agree about function vs.</div><div>non-function. &nbsp;It's not \
undefined behavior until you try to use a pointer to</div><div>member on an object \
whose dynamic type does not have that member.</div><div><br></div><div><blockquote \
type="cite"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: \
0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); \
border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; "> \
Notice that since Microsoft changes the size of the pointer depending on that<br> \
fact, it means it cannot be possible to point to a member of a derived \
class.</blockquote><div><br></div><div>No, it means Microsoft's implementation is \
non-conforming. See MS compiler warning&nbsp;C4407.</div></div> \
</blockquote></div><br><div>Precisely.</div><div><br></div><div>John.</div></body></html>




_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic