Topic: Virtual function for specific Base class


Author: Denis Kotov <redradist@gmail.com>
Date: Mon, 17 Sep 2018 13:43:18 -0700 (PDT)
Raw View
------=_Part_1865_1299697312.1537216998018
Content-Type: multipart/alternative;
 boundary="----=_Part_1866_173018184.1537216998019"

------=_Part_1866_173018184.1537216998019
Content-Type: text/plain; charset="UTF-8"

Hello every ones,

I wanna to share with you my future proposal and to hear some feedback on
it.


*The problem:*

class A {
 public:
  virtual void func() {
    std::cout << "A::func()" << std::endl;
  }
};

class B : public A {
 public:
  void func() override {
    std::cout << "B::func()" << std::endl;
  }
};

class C : public A {
 public:
  void func() override {
    std::cout << "C::func()" << std::endl;
  }
};

class D : public B, public C {
 public:
  void func() override {
    std::cout << "D::func()" << std::endl;
  }
};

For given class D it is not possible to override *func* only for specific class B:

D d;
B bb;
B & b = d;
A & aB = b;
C & c = d;
A & aC = c;
aB.func();
b.func();
aC.func();
c.func();

*Prints:*
D::func()
D::func()
D::func()
D::func()




*The Solution:*Adding the special kind of syntax it is possible to override *func* only for *B* or *C* classes

class A {
 public:
  virtual void func() {
    std::cout << "A::func()" << std::endl;
  }
};

class B : public A {
 public:
  void func() override {
    std::cout << "B::func()" << std::endl;
  }
};

class C : public A {
 public:
  void func() override {
    std::cout << "C::func()" << std::endl;
  }
};

class D : public B, public C {
 public:
  void B::func() override {
    std::cout << "D::B::func()" << std::endl;
  }
  void func() override {
    std::cout << "D::func()" << std::endl;
  }
};

D d;
B & b = d;
A & aB = b;
C & c = d;
A & aC = c;
aB.func();
b.func();
aC.func();
c.func();

*Prints:*
D::B::func()
D::B::func()
D::func()
D::func()

or for example to do something like this:

class A {
 public:
  virtual void func() {
    std::cout << "A::func()" << std::endl;
  }
};

class B : public A {
 public:
  void func() override {
    std::cout << "B::func()" << std::endl;
  }
};

class C : public A {
 public:
  void func() override {
    std::cout << "C::func()" << std::endl;
  }
};

class D : public B, public C {
 public:
  void B::func() override {
    std::cout << "D::B::func()" << std::endl;
  }
  void C::func() override {
    std::cout << "D::C::func()" << std::endl;
  }
};

D d;
B & b = d;
A & aB = b;
C & c = d;
A & aC = c;
aB.func();
b.func();
aC.func();
c.func();
d.func(); *// Error. Ambiguity !!*

*Prints:*
D::B::func()
D::B::func()
D::C::func()
D::C::func()

To fix the error you should do something like this:

class A {
 public:
  virtual void func() {
    std::cout << "A::func()" << std::endl;
  }
};

class B : public A {
 public:
  void func() override {
    std::cout << "B::func()" << std::endl;
  }
};

class C : public A {
 public:
  void func() override {
    std::cout << "C::func()" << std::endl;
  }
};

class D : public B, public C {
 public:
  void B::func() override {
    std::cout << "D::B::func()" << std::endl;
  }
  void C::func() override {
    std::cout << "D::C::func()" << std::endl;
  }
  void func() override {
    std::cout << "D::func()" << std::endl;
  }
};

D d;
B & b = d;
A & aB = b;
C & c = d;
A & aC = c;
aB.func();
b.func();
aC.func();
c.func();
d.func();  // Ok

*Prints:*
D::B::func()
D::B::func()
D::C::func()
D::C::func()
D::func()


I have already implemented something similar without new syntax be hacking the existing object structure:

D d;
uint8_t * dData = reinterpret_cast<uint8_t *>(&d);
B bb;
uint8_t * bbData = reinterpret_cast<uint8_t *>(&bb);
dData[0] = bbData[0];
dData[1] = bbData[1];
dData[2] = bbData[2];
dData[3] = bbData[3];
dData[4] = bbData[4];
dData[5] = bbData[5];
B & b = d;
A & aB = b;
C & c = d;
A & aC = c;
aB.func();
b.func();
aC.func();
c.func();

What do you think ?

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e314b158-45b2-476a-9463-e9b9d411eca1%40isocpp.org.

------=_Part_1866_173018184.1537216998019
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><font size=3D"4">Hello every ones,</font></div><div><=
font size=3D"4"><br></font></div><div><font size=3D"4">I wanna to share wit=
h you my future proposal and to hear some feedback on it.</font></div><div>=
<b><br></b></div><div><b><br></b></div><div><font size=3D"4"><b>The problem=
:</b></font></div><div><br></div><div><pre style=3D"background-color:#fffff=
f;color:#000000;font-family:&#39;DejaVu Sans Mono&#39;;font-size:11.3pt;"><=
span style=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight: bold;">cl=
ass </span>A {<br> <span style=3D"font-weight: bold;">public</span>:<br>  <=
span style=3D"font-weight: bold;">virtual void </span>func() {<br>    std::=
cout &lt;&lt; <span style=3D"font-weight: bold;">&quot;A::func()&quot; </sp=
an>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight: bold=
;">class </span>B : <span style=3D"font-weight: bold;">public </span>A {<br=
> <span style=3D"font-weight: bold;">public</span>:<br>  <span style=3D"fon=
t-weight: bold;">void </span>func() override {<br>    std::cout &lt;&lt; <s=
pan style=3D"font-weight: bold;">&quot;B::func()&quot; </span>&lt;&lt; std:=
:endl;<br>  }<br>};<br><br><span style=3D"font-weight: bold;">class </span>=
C : <span style=3D"font-weight: bold;">public </span>A {<br> <span style=3D=
"font-weight: bold;">public</span>:<br>  <span style=3D"font-weight: bold;"=
>void </span>func() override {<br>    std::cout &lt;&lt; <span style=3D"fon=
t-weight: bold;">&quot;C::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br=
>};<br><br><span style=3D"font-weight: bold;">class </span>D : <span style=
=3D"font-weight: bold;">public </span>B, <span style=3D"font-weight: bold;"=
>public </span>C {<br> <span style=3D"font-weight: bold;">public</span>:<br=
>  <span style=3D"font-weight: bold;">void </span>func() override {<br>    =
std::cout &lt;&lt; <span style=3D"font-weight: bold;">&quot;D::func()&quot;=
 </span>&lt;&lt; std::endl;<br>  }<br>};</span><br><br>For given class D it=
 is not possible to override <b>func</b> only for specific class <span styl=
e=3D"color:#000080;font-weight:bold;"></span><span style=3D"color:#008080;"=
>B:<br><br></span><span style=3D"color: rgb(0, 0, 255);">D d;<br>B bb;<br>B=
 &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D =
c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();</span><br><br><b>P=
rints:</b><br><span style=3D"color: rgb(0, 0, 255);">D::func()<br>D::func()=
<br>D::func()<br>D::func()</span><br><b><font size=3D"4"><br><br>The Soluti=
on:</font><br><br></b>Adding the special kind of syntax it is possible to o=
verride <b>func</b> only for <b>B</b> or <b>C</b> classes<br><br><span styl=
e=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight: bold;">class </spa=
n>A {<br> <span style=3D"font-weight: bold;">public</span>:<br>  <span styl=
e=3D"font-weight: bold;">virtual void </span>func() {<br>    std::cout &lt;=
&lt; <span style=3D"font-weight: bold;">&quot;A::func()&quot; </span>&lt;&l=
t; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight: bold;">class =
</span>B : <span style=3D"font-weight: bold;">public </span>A {<br> <span s=
tyle=3D"font-weight: bold;">public</span>:<br>  <span style=3D"font-weight:=
 bold;">void </span>func() override {<br>    std::cout &lt;&lt; <span style=
=3D"font-weight: bold;">&quot;B::func()&quot; </span>&lt;&lt; std::endl;<br=
>  }<br>};<br><br><span style=3D"font-weight: bold;">class </span>C : <span=
 style=3D"font-weight: bold;">public </span>A {<br> <span style=3D"font-wei=
ght: bold;">public</span>:<br>  <span style=3D"font-weight: bold;">void </s=
pan>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-weight:=
 bold;">&quot;C::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><b=
r><span style=3D"font-weight: bold;">class </span>D : <span style=3D"font-w=
eight: bold;">public </span>B, <span style=3D"font-weight: bold;">public </=
span>C {<br> <span style=3D"font-weight: bold;">public</span>:<br>  <span s=
tyle=3D"font-weight: bold;">void B::</span>func() override {<br>    std::co=
ut &lt;&lt; <span style=3D"font-weight: bold;">&quot;D::B::</span><span sty=
le=3D"font-weight: bold;"><span style=3D"font-weight: bold;"></span>func()&=
quot; </span>&lt;&lt; std::endl;<br>  }<br>  <span style=3D"font-weight: bo=
ld;">void </span>func() override {<br>    std::cout &lt;&lt; <span style=3D=
"font-weight: bold;">&quot;D::func()&quot; </span>&lt;&lt; std::endl;<br>  =
}<br>};</span><br><br><span style=3D"color: rgb(0, 0, 255);">D d;<br>B &amp=
; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c;<br=
>aB.func();<br>b.func();<br>aC.func();<br>c.func();</span><br><br><b>Prints=
:</b><br><span style=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight:=
 bold;">D::B::</span><span style=3D"font-weight: bold;"><span style=3D"font=
-weight: bold;"></span>func()</span><br><span style=3D"font-weight: bold;">=
D::B::</span><span style=3D"font-weight: bold;"><span style=3D"font-weight:=
 bold;"></span>func()</span><br><span style=3D"font-weight: bold;">D::func(=
)</span><br><span style=3D"font-weight: bold;">D::func()</span></span><br><=
br>or for example to do something like this:<br><br><span style=3D"color: r=
gb(0, 0, 255);"><span style=3D"font-weight: bold;">class </span>A {<br> <sp=
an style=3D"font-weight: bold;">public</span>:<br>  <span style=3D"font-wei=
ght: bold;">virtual void </span>func() {<br>    std::cout &lt;&lt; <span st=
yle=3D"font-weight: bold;">&quot;A::func()&quot; </span>&lt;&lt; std::endl;=
<br>  }<br>};<br><br><span style=3D"font-weight: bold;">class </span>B : <s=
pan style=3D"font-weight: bold;">public </span>A {<br> <span style=3D"font-=
weight: bold;">public</span>:<br>  <span style=3D"font-weight: bold;">void =
</span>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-weig=
ht: bold;">&quot;B::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br=
><br><span style=3D"font-weight: bold;">class </span>C : <span style=3D"fon=
t-weight: bold;">public </span>A {<br> <span style=3D"font-weight: bold;">p=
ublic</span>:<br>  <span style=3D"font-weight: bold;">void </span>func() ov=
erride {<br>    std::cout &lt;&lt; <span style=3D"font-weight: bold;">&quot=
;C::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=
=3D"font-weight: bold;">class </span>D : <span style=3D"font-weight: bold;"=
>public </span>B, <span style=3D"font-weight: bold;">public </span>C {<br> =
<span style=3D"font-weight: bold;">public</span>:<br>  <span style=3D"font-=
weight: bold;">void B::</span>func() override {<br>    std::cout &lt;&lt; <=
span style=3D"font-weight: bold;">&quot;D::B::</span><span style=3D"font-we=
ight: bold;"><span style=3D"font-weight: bold;"></span>func()&quot; </span>=
&lt;&lt; std::endl;<br>  }<br>  <span style=3D"font-weight: bold;">void </s=
pan><span style=3D"font-weight: bold;"></span>C::func() override {<br>    s=
td::cout &lt;&lt; <span style=3D"font-weight: bold;">&quot;D::C::func()&quo=
t; </span>&lt;&lt; std::endl;<br>  }<br>};</span><br><br><span style=3D"col=
or: rgb(0, 0, 255);">D d;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &am=
p; c =3D d;<br>A &amp; aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<b=
r>c.func();<br>d.func();</span> <b>// <span style=3D"color: rgb(255, 0, 0);=
">Error. Ambiguity !!</span></b><br><br><b>Prints:</b><br><span style=3D"co=
lor: rgb(0, 0, 255);"><span style=3D"font-weight: bold;">D::B::</span><span=
 style=3D"font-weight: bold;"><span style=3D"font-weight: bold;"></span>fun=
c()</span><span style=3D"font-weight: bold;"></span><br><span style=3D"font=
-weight: bold;">D::B::</span><span style=3D"font-weight: bold;"><span style=
=3D"font-weight: bold;"></span>func()</span><span style=3D"font-weight: bol=
d;"></span><br><span style=3D"font-weight: bold;">D::C::func()</span><br></=
span><span style=3D"color:#008000;font-weight:bold;"><span style=3D"color: =
rgb(0, 0, 255);"><span style=3D"font-weight: bold;">D::C::func()</span></sp=
an><br><br></span><span style=3D"color: rgb(0, 128, 0);"><span style=3D"col=
or: rgb(0, 0, 0);">To fix the error you should do something like this:</spa=
n></span><span style=3D"color:#008000;font-weight:bold;"><br></span><br><sp=
an style=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight: bold;"><spa=
n style=3D"font-weight: bold;">class </span>A {<br> <span style=3D"font-wei=
ght: bold;">public</span>:<br>  <span style=3D"font-weight: bold;">virtual =
void </span>func() {<br>    std::cout &lt;&lt; <span style=3D"font-weight: =
bold;">&quot;A::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br=
><span style=3D"font-weight: bold;">class </span>B : <span style=3D"font-we=
ight: bold;">public </span>A {<br> <span style=3D"font-weight: bold;">publi=
c</span>:<br>  <span style=3D"font-weight: bold;">void </span>func() overri=
de {<br>    std::cout &lt;&lt; <span style=3D"font-weight: bold;">&quot;B::=
func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"=
font-weight: bold;">class </span>C : <span style=3D"font-weight: bold;">pub=
lic </span>A {<br> <span style=3D"font-weight: bold;">public</span>:<br>  <=
span style=3D"font-weight: bold;">void </span>func() override {<br>    std:=
:cout &lt;&lt; <span style=3D"font-weight: bold;">&quot;C::func()&quot; </s=
pan>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight: bol=
d;">class </span>D : <span style=3D"font-weight: bold;">public </span>B, <s=
pan style=3D"font-weight: bold;">public </span>C {<br> <span style=3D"font-=
weight: bold;">public</span>:<br>  <span style=3D"font-weight: bold;">void =
B::</span>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-w=
eight: bold;">&quot;D::B::</span><span style=3D"font-weight: bold;"><span s=
tyle=3D"font-weight: bold;"></span>func()&quot; </span>&lt;&lt; std::endl;<=
br>  }<br>  <span style=3D"font-weight: bold;">void </span><span style=3D"f=
ont-weight: bold;"></span>C::func() override {<br>    std::cout &lt;&lt; <s=
pan style=3D"font-weight: bold;">&quot;D::C::func()&quot; </span>&lt;&lt; s=
td::endl;<br>  }</span><br></span><span style=3D"color:#008000;font-weight:=
bold;"><span style=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight: b=
old;">  <span style=3D"font-weight: bold;">void </span><span style=3D"font-=
weight: bold;"></span>func() override {<br>    std::cout &lt;&lt; <span sty=
le=3D"font-weight: bold;">&quot;D::func()&quot; </span>&lt;&lt; std::endl;<=
br>  }<br></span>};</span><br><br><span style=3D"color: rgb(0, 0, 255);">D =
d;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; =
aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();<br>d.func()=
;</span>  // Ok<br><br><span style=3D"color: rgb(0, 0, 0);"><b>Prints:</b><=
/span><br><span style=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight=
: bold;">D::B::</span><span style=3D"font-weight: bold;"><span style=3D"fon=
t-weight: bold;"></span>func()</span><span style=3D"font-weight: bold;"></s=
pan><br><span style=3D"font-weight: bold;">D::B::</span><span style=3D"font=
-weight: bold;"><span style=3D"font-weight: bold;"></span>func()</span><spa=
n style=3D"font-weight: bold;"></span><br><span style=3D"font-weight: bold;=
">D::C::func()</span><br><span style=3D"font-weight: bold;"><span style=3D"=
font-weight: bold;">D::C::func()<br></span></span></span></span><span style=
=3D"color:#008000;font-weight:bold;"><span style=3D"color:#008000;font-weig=
ht:bold;"><span style=3D"color: rgb(0, 0, 0);"><span style=3D"color:#008000=
;font-weight:bold;"><span style=3D"color: rgb(0, 0, 255);">D::func()</span>=
<br><br><br></span></span></span></span><span style=3D"color: rgb(0, 128, 0=
);"><span style=3D"color: rgb(0, 128, 0);"><span style=3D"color: rgb(0, 0, =
0);"><span style=3D"color: rgb(0, 128, 0);"><span style=3D"color: rgb(0, 0,=
 0);">I have already implemented something similar without new syntax be ha=
cking the existing object structure:</span></span></span></span></span><spa=
n style=3D"color:#008000;font-weight:bold;"><span style=3D"color:#008000;fo=
nt-weight:bold;"><span style=3D"color: rgb(0, 0, 0);"><span style=3D"color:=
#008000;font-weight:bold;"><br></span></span></span></span><br><span style=
=3D"color: rgb(0, 0, 255);"><span style=3D"font-weight: bold;"><span style=
=3D"font-weight: bold;"><span style=3D"font-weight: bold;">D d;<br>uint8_t =
* dData =3D <span style=3D"font-weight: bold;">reinterpret_cast</span>&lt;u=
int8_t *&gt;(&amp;d);<br>B bb;<br>uint8_t * bbData =3D <span style=3D"font-=
weight: bold;">reinterpret_cast</span>&lt;uint8_t *&gt;(&amp;bb);<br>dData[=
0] =3D bbData[0];<br>dData[1] =3D bbData[1];<br>dData[2] =3D bbData[2];<br>=
dData[3] =3D bbData[3];<br>dData[4] =3D bbData[4];<br>dData[5] =3D bbData[5=
];<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; =
aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();<br></span><=
/span></span><br><span style=3D"color: rgb(0, 0, 0);">What do you think ?</=
span></span><span style=3D"color:#008080;"></span></pre></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e314b158-45b2-476a-9463-e9b9d411eca1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e314b158-45b2-476a-9463-e9b9d411eca1=
%40isocpp.org</a>.<br />

------=_Part_1866_173018184.1537216998019--

------=_Part_1865_1299697312.1537216998018--

.


Author: Edward Catmur <ed@catmur.co.uk>
Date: Mon, 17 Sep 2018 15:54:02 -0700 (PDT)
Raw View
------=_Part_2043_1542286658.1537224843019
Content-Type: multipart/alternative;
 boundary="----=_Part_2044_680051340.1537224843020"

------=_Part_2044_680051340.1537224843020
Content-Type: text/plain; charset="UTF-8"



On Monday, 17 September 2018 21:43:18 UTC+1, Denis Kotov wrote:
>
> Hello every ones,
>
> I wanna to share with you my future proposal and to hear some feedback on
> it.
>
>
> *The problem:*
>
> class A {
>  public:
>   virtual void func() {
>     std::cout << "A::func()" << std::endl;
>   }
> };
>
> class B : public A {
>  public:
>   void func() override {
>     std::cout << "B::func()" << std::endl;
>   }
> };
>
> class C : public A {
>  public:
>   void func() override {
>     std::cout << "C::func()" << std::endl;
>   }
> };
>
> class D : public B, public C {
>  public:
>   void func() override {
>     std::cout << "D::func()" << std::endl;
>   }
> };
>
> For given class D it is not possible to override *func* only for specific class B:
>
> D d;
> B bb;
> B & b = d;
> A & aB = b;
> C & c = d;
> A & aC = c;
> aB.func();
> b.func();
> aC.func();
> c.func();
>
> *Prints:*
> D::func()
> D::func()
> D::func()
> D::func()
>
>
>
>
> *The Solution:*Adding the special kind of syntax it is possible to override *func* only for *B* or *C* classes
>
> class A {
>  public:
>   virtual void func() {
>     std::cout << "A::func()" << std::endl;
>   }
> };
>
> class B : public A {
>  public:
>   void func() override {
>     std::cout << "B::func()" << std::endl;
>   }
> };
>
> class C : public A {
>  public:
>   void func() override {
>     std::cout << "C::func()" << std::endl;
>   }
> };
>
> class D : public B, public C {
>  public:
>   void B::func() override {
>     std::cout << "D::B::func()" << std::endl;
>   }
>   void func() override {
>     std::cout << "D::func()" << std::endl;
>   }
> };
>
> D d;
> B & b = d;
> A & aB = b;
> C & c = d;
> A & aC = c;
> aB.func();
> b.func();
> aC.func();
> c.func();
>
> *Prints:*
> D::B::func()
> D::B::func()
> D::func()
> D::func()
>
> or for example to do something like this:
>
> class A {
>  public:
>   virtual void func() {
>     std::cout << "A::func()" << std::endl;
>   }
> };
>
> class B : public A {
>  public:
>   void func() override {
>     std::cout << "B::func()" << std::endl;
>   }
> };
>
> class C : public A {
>  public:
>   void func() override {
>     std::cout << "C::func()" << std::endl;
>   }
> };
>
> class D : public B, public C {
>  public:
>   void B::func() override {
>     std::cout << "D::B::func()" << std::endl;
>   }
>   void C::func() override {
>     std::cout << "D::C::func()" << std::endl;
>   }
> };
>
>
p0945 "Generalizing alias declarations" provides a more general syntax that
covers this use case among others. See the final example under
http://wg21.link/p0945#member-aliases .


> D d;
> B & b = d;
> A & aB = b;
> C & c = d;
> A & aC = c;
> aB.func();
> b.func();
> aC.func();
> c.func();
> d.func(); *// Error. Ambiguity !!*
>
> *Prints:*
> D::B::func()
> D::B::func()
> D::C::func()
> D::C::func()
>
> To fix the error you should do something like this:
>
> class A {
>  public:
>   virtual void func() {
>     std::cout << "A::func()" << std::endl;
>   }
> };
>
> class B : public A {
>  public:
>   void func() override {
>     std::cout << "B::func()" << std::endl;
>   }
> };
>
> class C : public A {
>  public:
>   void func() override {
>     std::cout << "C::func()" << std::endl;
>   }
> };
>
> class D : public B, public C {
>  public:
>   void B::func() override {
>     std::cout << "D::B::func()" << std::endl;
>   }
>   void C::func() override {
>     std::cout << "D::C::func()" << std::endl;
>   }
>   void func() override {
>     std::cout << "D::func()" << std::endl;
>   }
> };
>
> D d;
> B & b = d;
> A & aB = b;
> C & c = d;
> A & aC = c;
> aB.func();
> b.func();
> aC.func();
> c.func();
> d.func();  // Ok
>
> *Prints:*
> D::B::func()
> D::B::func()
> D::C::func()
> D::C::func()
> D::func()
>
>
> I have already implemented something similar without new syntax be hacking the existing object structure:
>
> D d;
> uint8_t * dData = reinterpret_cast<uint8_t *>(&d);
> B bb;
> uint8_t * bbData = reinterpret_cast<uint8_t *>(&bb);
> dData[0] = bbData[0];
> dData[1] = bbData[1];
> dData[2] = bbData[2];
> dData[3] = bbData[3];
> dData[4] = bbData[4];
> dData[5] = bbData[5];
> B & b = d;
> A & aB = b;
> C & c = d;
> A & aC = c;
> aB.func();
> b.func();
> aC.func();
> c.func();
>
> What do you think ?
>
>

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/df43cc7b-a8fc-4223-927f-269acae10fcd%40isocpp.org.

------=_Part_2044_680051340.1537224843020
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Monday, 17 September 2018 21:43:18 UTC+1, Denis=
 Kotov  wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"=
><div><font size=3D"4">Hello every ones,</font></div><div><font size=3D"4">=
<br></font></div><div><font size=3D"4">I wanna to share with you my future =
proposal and to hear some feedback on it.</font></div><div><b><br></b></div=
><div><b><br></b></div><div><font size=3D"4"><b>The problem:</b></font></di=
v><div><br></div><div><pre style=3D"background-color:#ffffff;color:#000000;=
font-family:&#39;DejaVu Sans Mono&#39;;font-size:11.3pt"><span style=3D"col=
or:rgb(0,0,255)"><span style=3D"font-weight:bold">class </span>A {<br> <spa=
n style=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weight=
:bold">virtual void </span>func() {<br>    std::cout &lt;&lt; <span style=
=3D"font-weight:bold">&quot;A::func()&quot; </span>&lt;&lt; std::endl;<br> =
 }<br>};<br><br><span style=3D"font-weight:bold">class </span>B : <span sty=
le=3D"font-weight:bold">public </span>A {<br> <span style=3D"font-weight:bo=
ld">public</span>:<br>  <span style=3D"font-weight:bold">void </span>func()=
 override {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quo=
t;B::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span styl=
e=3D"font-weight:bold">class </span>C : <span style=3D"font-weight:bold">pu=
blic </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br>  <s=
pan style=3D"font-weight:bold">void </span>func() override {<br>    std::co=
ut &lt;&lt; <span style=3D"font-weight:bold">&quot;C::func()&quot; </span>&=
lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">cla=
ss </span>D : <span style=3D"font-weight:bold">public </span>B, <span style=
=3D"font-weight:bold">public </span>C {<br> <span style=3D"font-weight:bold=
">public</span>:<br>  <span style=3D"font-weight:bold">void </span>func() o=
verride {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;=
D::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};</span><br><br>For g=
iven class D it is not possible to override <b>func</b> only for specific c=
lass <span style=3D"color:#000080;font-weight:bold"></span><span style=3D"c=
olor:#008080">B:<br><br></span><span style=3D"color:rgb(0,0,255)">D d;<br>B=
 bb;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp=
; aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();</span><br=
><br><b>Prints:</b><br><span style=3D"color:rgb(0,0,255)">D::func()<br>D::f=
unc()<br>D::func()<br>D::func()</span><br><b><font size=3D"4"><br><br>The S=
olution:</font><br><br></b>Adding the special kind of syntax it is possible=
 to override <b>func</b> only for <b>B</b> or <b>C</b> classes<br><br><span=
 style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold">class </span=
>A {<br> <span style=3D"font-weight:bold">public</span>:<br>  <span style=
=3D"font-weight:bold">virtual void </span>func() {<br>    std::cout &lt;&lt=
; <span style=3D"font-weight:bold">&quot;A::func()&quot; </span>&lt;&lt; st=
d::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">class </span>=
B : <span style=3D"font-weight:bold">public </span>A {<br> <span style=3D"f=
ont-weight:bold">public</span>:<br>  <span style=3D"font-weight:bold">void =
</span>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-weig=
ht:bold">&quot;B::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><=
br><span style=3D"font-weight:bold">class </span>C : <span style=3D"font-we=
ight:bold">public </span>A {<br> <span style=3D"font-weight:bold">public</s=
pan>:<br>  <span style=3D"font-weight:bold">void </span>func() override {<b=
r>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;C::func()&q=
uot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-wei=
ght:bold">class </span>D : <span style=3D"font-weight:bold">public </span>B=
, <span style=3D"font-weight:bold">public </span>C {<br> <span style=3D"fon=
t-weight:bold">public</span>:<br>  <span style=3D"font-weight:bold">void B:=
:</span>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-wei=
ght:bold">&quot;D::B::</span><span style=3D"font-weight:bold"><span style=
=3D"font-weight:bold"></span>func()&quot; </span>&lt;&lt; std::endl;<br>  }=
<br>  <span style=3D"font-weight:bold">void </span>func() override {<br>   =
 std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::func()&quot; =
</span>&lt;&lt; std::endl;<br>  }<br>};</span><br><br><span style=3D"color:=
rgb(0,0,255)">D d;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =
=3D d;<br>A &amp; aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.f=
unc();</span><br><br><b>Prints:</b><br><span style=3D"color:rgb(0,0,255)"><=
span style=3D"font-weight:bold">D::B::</span><span style=3D"font-weight:bol=
d"><span style=3D"font-weight:bold"></span>func()</span><br><span style=3D"=
font-weight:bold">D::B::</span><span style=3D"font-weight:bold"><span style=
=3D"font-weight:bold"></span>func()</span><br><span style=3D"font-weight:bo=
ld">D::func()</span><br><span style=3D"font-weight:bold">D::func()</span></=
span><br><br>or for example to do something like this:<br><br><span style=
=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold">class </span>A {<b=
r> <span style=3D"font-weight:bold">public</span>:<br>  <span style=3D"font=
-weight:bold">virtual void </span>func() {<br>    std::cout &lt;&lt; <span =
style=3D"font-weight:bold">&quot;A::func()&quot; </span>&lt;&lt; std::endl;=
<br>  }<br>};<br><br><span style=3D"font-weight:bold">class </span>B : <spa=
n style=3D"font-weight:bold">public </span>A {<br> <span style=3D"font-weig=
ht:bold">public</span>:<br>  <span style=3D"font-weight:bold">void </span>f=
unc() override {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold"=
>&quot;B::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span=
 style=3D"font-weight:bold">class </span>C : <span style=3D"font-weight:bol=
d">public </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br=
>  <span style=3D"font-weight:bold">void </span>func() override {<br>    st=
d::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;C::func()&quot; </s=
pan>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold=
">class </span>D : <span style=3D"font-weight:bold">public </span>B, <span =
style=3D"font-weight:bold">public </span>C {<br> <span style=3D"font-weight=
:bold">public</span>:<br>  <span style=3D"font-weight:bold">void B::</span>=
func() override {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold=
">&quot;D::B::</span><span style=3D"font-weight:bold"><span style=3D"font-w=
eight:bold"></span>func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>  <spa=
n style=3D"font-weight:bold">void </span><span style=3D"font-weight:bold"><=
/span>C::func() override {<br>    std::cout &lt;&lt; <span style=3D"font-we=
ight:bold">&quot;D::C::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};=
</span></pre></div></div></blockquote><div><br></div><div><h1 class=3D"p-na=
me no-ref" id=3D"title" style=3D"break-after: avoid; break-inside: avoid; f=
ont-variant-numeric: normal; font-variant-east-asian: normal; font-stretch:=
 normal; line-height: 1.2; hyphens: manual; background-image: initial; back=
ground-position: initial; background-size: initial; background-repeat: init=
ial; background-attachment: initial; background-origin: initial; background=
-clip: initial;"><font size=3D"2" style=3D"font-family: sans-serif;"><font =
style=3D"" color=3D"#000000">p0945 &quot;</font>Generalizing alias declarat=
ions&quot; provides a more general syntax that covers this use case among o=
thers. See the final example under http://wg21.link/p0945#</font><font face=
=3D"sans-serif" size=3D"2">member-aliases .</font></h1></div><div>=C2=A0</d=
iv><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;=
border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><pre =
style=3D"background-color:#ffffff;color:#000000;font-family:&#39;DejaVu San=
s Mono&#39;;font-size:11.3pt"><span style=3D"color:rgb(0,0,255)">D d;<br>B =
&amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c=
;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();<br>d.func();</span>=
 <b>// <span style=3D"color:rgb(255,0,0)">Error. Ambiguity !!</span></b><br=
><br><b>Prints:</b><br><span style=3D"color:rgb(0,0,255)"><span style=3D"fo=
nt-weight:bold">D::B::</span><span style=3D"font-weight:bold"><span style=
=3D"font-weight:bold"></span>func()</span><span style=3D"font-weight:bold">=
</span><br><span style=3D"font-weight:bold">D::B::</span><span style=3D"fon=
t-weight:bold"><span style=3D"font-weight:bold"></span>func()</span><span s=
tyle=3D"font-weight:bold"></span><br><span style=3D"font-weight:bold">D::C:=
:func()</span><br></span><span style=3D"color:#008000;font-weight:bold"><sp=
an style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold">D::C::func=
()</span></span><br><br></span><span style=3D"color:rgb(0,128,0)"><span sty=
le=3D"color:rgb(0,0,0)">To fix the error you should do something like this:=
</span></span><span style=3D"color:#008000;font-weight:bold"><br></span><br=
><span style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold"><span =
style=3D"font-weight:bold">class </span>A {<br> <span style=3D"font-weight:=
bold">public</span>:<br>  <span style=3D"font-weight:bold">virtual void </s=
pan>func() {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&qu=
ot;A::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span sty=
le=3D"font-weight:bold">class </span>B : <span style=3D"font-weight:bold">p=
ublic </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br>  <=
span style=3D"font-weight:bold">void </span>func() override {<br>    std::c=
out &lt;&lt; <span style=3D"font-weight:bold">&quot;B::func()&quot; </span>=
&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">cl=
ass </span>C : <span style=3D"font-weight:bold">public </span>A {<br> <span=
 style=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weight:=
bold">void </span>func() override {<br>    std::cout &lt;&lt; <span style=
=3D"font-weight:bold">&quot;C::func()&quot; </span>&lt;&lt; std::endl;<br> =
 }<br>};<br><br><span style=3D"font-weight:bold">class </span>D : <span sty=
le=3D"font-weight:bold">public </span>B, <span style=3D"font-weight:bold">p=
ublic </span>C {<br> <span style=3D"font-weight:bold">public</span>:<br>  <=
span style=3D"font-weight:bold">void B::</span>func() override {<br>    std=
::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::B::</span><span s=
tyle=3D"font-weight:bold"><span style=3D"font-weight:bold"></span>func()&qu=
ot; </span>&lt;&lt; std::endl;<br>  }<br>  <span style=3D"font-weight:bold"=
>void </span><span style=3D"font-weight:bold"></span>C::func() override {<b=
r>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::C::func(=
)&quot; </span>&lt;&lt; std::endl;<br>  }</span><br></span><span style=3D"c=
olor:#008000;font-weight:bold"><span style=3D"color:rgb(0,0,255)"><span sty=
le=3D"font-weight:bold">  <span style=3D"font-weight:bold">void </span><spa=
n style=3D"font-weight:bold"></span>func() override {<br>    std::cout &lt;=
&lt; <span style=3D"font-weight:bold">&quot;D::func()&quot; </span>&lt;&lt;=
 std::endl;<br>  }<br></span>};</span><br><br><span style=3D"color:rgb(0,0,=
255)">D d;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>=
A &amp; aC =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();<br>=
d.func();</span>  // Ok<br><br><span style=3D"color:rgb(0,0,0)"><b>Prints:<=
/b></span><br><span style=3D"color:rgb(0,0,255)"><span style=3D"font-weight=
:bold">D::B::</span><span style=3D"font-weight:bold"><span style=3D"font-we=
ight:bold"></span>func()</span><span style=3D"font-weight:bold"></span><br>=
<span style=3D"font-weight:bold">D::B::</span><span style=3D"font-weight:bo=
ld"><span style=3D"font-weight:bold"></span>func()</span><span style=3D"fon=
t-weight:bold"></span><br><span style=3D"font-weight:bold">D::C::func()</sp=
an><br><span style=3D"font-weight:bold"><span style=3D"font-weight:bold">D:=
:C::func()<br></span></span></span></span><span style=3D"color:#008000;font=
-weight:bold"><span style=3D"color:#008000;font-weight:bold"><span style=3D=
"color:rgb(0,0,0)"><span style=3D"color:#008000;font-weight:bold"><span sty=
le=3D"color:rgb(0,0,255)">D::func()</span><br><br><br></span></span></span>=
</span><span style=3D"color:rgb(0,128,0)"><span style=3D"color:rgb(0,128,0)=
"><span style=3D"color:rgb(0,0,0)"><span style=3D"color:rgb(0,128,0)"><span=
 style=3D"color:rgb(0,0,0)">I have already implemented something similar wi=
thout new syntax be hacking the existing object structure:</span></span></s=
pan></span></span><span style=3D"color:#008000;font-weight:bold"><span styl=
e=3D"color:#008000;font-weight:bold"><span style=3D"color:rgb(0,0,0)"><span=
 style=3D"color:#008000;font-weight:bold"><br></span></span></span></span><=
br><span style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold"><spa=
n style=3D"font-weight:bold"><span style=3D"font-weight:bold">D d;<br>uint8=
_t * dData =3D <span style=3D"font-weight:bold">reinterpret_cast</span>&lt;=
uint8_t *&gt;(&amp;d);<br>B bb;<br>uint8_t * bbData =3D <span style=3D"font=
-weight:bold">reinterpret_cast</span>&lt;uint8_t *&gt;(&amp;bb);<br>dData[0=
] =3D bbData[0];<br>dData[1] =3D bbData[1];<br>dData[2] =3D bbData[2];<br>d=
Data[3] =3D bbData[3];<br>dData[4] =3D bbData[4];<br>dData[5] =3D bbData[5]=
;<br>B &amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; a=
C =3D c;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();<br></span></=
span></span><br><span style=3D"color:rgb(0,0,0)">What do you think ?</span>=
</span></pre></div></div></blockquote><div><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/df43cc7b-a8fc-4223-927f-269acae10fcd%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/df43cc7b-a8fc-4223-927f-269acae10fcd=
%40isocpp.org</a>.<br />

------=_Part_2044_680051340.1537224843020--

------=_Part_2043_1542286658.1537224843019--

.


Author: Thiago Macieira <thiago@macieira.org>
Date: Mon, 17 Sep 2018 21:16:28 -0700
Raw View
On Monday, 17 September 2018 13:43:18 PDT Denis Kotov wrote:
> For given class D it is not possible to override *func* only for specific
> class B:

I believe that's intentional. Functions of the same name are meant to do the
same thing. The override is meant to be of all.

If you really need this, you can achieve it with an intermediate class

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center



--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3260689.nmoMtFEJCx%40tjmaciei-mobl1.

.


Author: Denis Kotov <redradist@gmail.com>
Date: Tue, 18 Sep 2018 00:07:06 -0700 (PDT)
Raw View
------=_Part_2027_92558354.1537254426138
Content-Type: multipart/alternative;
 boundary="----=_Part_2028_1125004924.1537254426139"

------=_Part_2028_1125004924.1537254426139
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Yes, according the proposal it is possible, but for overriding specific=20
virtual method it will create alias noise.

I would prefer to override method for specific class without creating noise=
=20
for virtual methods (a lot of aliases for all virtual method).

=D0=B2=D1=82=D0=BE=D1=80=D0=BD=D0=B8=D0=BA, 18 =D1=81=D0=B5=D0=BD=D1=82=D1=
=8F=D0=B1=D1=80=D1=8F 2018 =D0=B3., 1:54:03 UTC+3 =D0=BF=D0=BE=D0=BB=D1=8C=
=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8C Edward Catmur=20
=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB:
>
>
>
> On Monday, 17 September 2018 21:43:18 UTC+1, Denis Kotov wrote:
>>
>> Hello every ones,
>>
>> I wanna to share with you my future proposal and to hear some feedback o=
n=20
>> it.
>>
>>
>> *The problem:*
>>
>> class A {
>>  public:
>>   virtual void func() {
>>     std::cout << "A::func()" << std::endl;
>>   }
>> };
>>
>> class B : public A {
>>  public:
>>   void func() override {
>>     std::cout << "B::func()" << std::endl;
>>   }
>> };
>>
>> class C : public A {
>>  public:
>>   void func() override {
>>     std::cout << "C::func()" << std::endl;
>>   }
>> };
>>
>> class D : public B, public C {
>>  public:
>>   void func() override {
>>     std::cout << "D::func()" << std::endl;
>>   }
>> };
>>
>> For given class D it is not possible to override *func* only for specifi=
c class B:
>>
>> D d;
>> B bb;
>> B & b =3D d;
>> A & aB =3D b;
>> C & c =3D d;
>> A & aC =3D c;
>> aB.func();
>> b.func();
>> aC.func();
>> c.func();
>>
>> *Prints:*
>> D::func()
>> D::func()
>> D::func()
>> D::func()
>>
>>
>>
>>
>> *The Solution:*Adding the special kind of syntax it is possible to overr=
ide *func* only for *B* or *C* classes
>>
>> class A {
>>  public:
>>   virtual void func() {
>>     std::cout << "A::func()" << std::endl;
>>   }
>> };
>>
>> class B : public A {
>>  public:
>>   void func() override {
>>     std::cout << "B::func()" << std::endl;
>>   }
>> };
>>
>> class C : public A {
>>  public:
>>   void func() override {
>>     std::cout << "C::func()" << std::endl;
>>   }
>> };
>>
>> class D : public B, public C {
>>  public:
>>   void B::func() override {
>>     std::cout << "D::B::func()" << std::endl;
>>   }
>>   void func() override {
>>     std::cout << "D::func()" << std::endl;
>>   }
>> };
>>
>> D d;
>> B & b =3D d;
>> A & aB =3D b;
>> C & c =3D d;
>> A & aC =3D c;
>> aB.func();
>> b.func();
>> aC.func();
>> c.func();
>>
>> *Prints:*
>> D::B::func()
>> D::B::func()
>> D::func()
>> D::func()
>>
>> or for example to do something like this:
>>
>> class A {
>>  public:
>>   virtual void func() {
>>     std::cout << "A::func()" << std::endl;
>>   }
>> };
>>
>> class B : public A {
>>  public:
>>   void func() override {
>>     std::cout << "B::func()" << std::endl;
>>   }
>> };
>>
>> class C : public A {
>>  public:
>>   void func() override {
>>     std::cout << "C::func()" << std::endl;
>>   }
>> };
>>
>> class D : public B, public C {
>>  public:
>>   void B::func() override {
>>     std::cout << "D::B::func()" << std::endl;
>>   }
>>   void C::func() override {
>>     std::cout << "D::C::func()" << std::endl;
>>   }
>> };
>>
>>
> p0945 "Generalizing alias declarations" provides a more general syntax=20
> that covers this use case among others. See the final example under=20
> http://wg21.link/p0945#member-aliases .
> =20
>
>> D d;
>> B & b =3D d;
>> A & aB =3D b;
>> C & c =3D d;
>> A & aC =3D c;
>> aB.func();
>> b.func();
>> aC.func();
>> c.func();
>> d.func(); *// Error. Ambiguity !!*
>>
>> *Prints:*
>> D::B::func()
>> D::B::func()
>> D::C::func()
>> D::C::func()
>>
>> To fix the error you should do something like this:
>>
>> class A {
>>  public:
>>   virtual void func() {
>>     std::cout << "A::func()" << std::endl;
>>   }
>> };
>>
>> class B : public A {
>>  public:
>>   void func() override {
>>     std::cout << "B::func()" << std::endl;
>>   }
>> };
>>
>> class C : public A {
>>  public:
>>   void func() override {
>>     std::cout << "C::func()" << std::endl;
>>   }
>> };
>>
>> class D : public B, public C {
>>  public:
>>   void B::func() override {
>>     std::cout << "D::B::func()" << std::endl;
>>   }
>>   void C::func() override {
>>     std::cout << "D::C::func()" << std::endl;
>>   }
>>   void func() override {
>>     std::cout << "D::func()" << std::endl;
>>   }
>> };
>>
>> D d;
>> B & b =3D d;
>> A & aB =3D b;
>> C & c =3D d;
>> A & aC =3D c;
>> aB.func();
>> b.func();
>> aC.func();
>> c.func();
>> d.func();  // Ok
>>
>> *Prints:*
>> D::B::func()
>> D::B::func()
>> D::C::func()
>> D::C::func()
>> D::func()
>>
>>
>> I have already implemented something similar without new syntax be hacki=
ng the existing object structure:
>>
>> D d;
>> uint8_t * dData =3D reinterpret_cast<uint8_t *>(&d);
>> B bb;
>> uint8_t * bbData =3D reinterpret_cast<uint8_t *>(&bb);
>> dData[0] =3D bbData[0];
>> dData[1] =3D bbData[1];
>> dData[2] =3D bbData[2];
>> dData[3] =3D bbData[3];
>> dData[4] =3D bbData[4];
>> dData[5] =3D bbData[5];
>> B & b =3D d;
>> A & aB =3D b;
>> C & c =3D d;
>> A & aC =3D c;
>> aB.func();
>> b.func();
>> aC.func();
>> c.func();
>>
>> What do you think ?
>>
>>
>

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/0415ffeb-e4ea-4e2f-9f55-03d9110ddfd1%40isocpp.or=
g.

------=_Part_2028_1125004924.1537254426139
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Yes, according the proposal it is possible, but for overri=
ding specific virtual method it will create alias noise.<br><br>I would pre=
fer to override method for specific class without creating noise for virtua=
l methods (a lot of aliases for all virtual method).<br><br>=D0=B2=D1=82=D0=
=BE=D1=80=D0=BD=D0=B8=D0=BA, 18 =D1=81=D0=B5=D0=BD=D1=82=D1=8F=D0=B1=D1=80=
=D1=8F 2018 =D0=B3., 1:54:03 UTC+3 =D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=
=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8C Edward Catmur =D0=BD=D0=B0=D0=BF=D0=B8=D1=
=81=D0=B0=D0=BB:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin=
-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"lt=
r"><br><br>On Monday, 17 September 2018 21:43:18 UTC+1, Denis Kotov  wrote:=
<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;borde=
r-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><font size=3D=
"4">Hello every ones,</font></div><div><font size=3D"4"><br></font></div><d=
iv><font size=3D"4">I wanna to share with you my future proposal and to hea=
r some feedback on it.</font></div><div><b><br></b></div><div><b><br></b></=
div><div><font size=3D"4"><b>The problem:</b></font></div><div><br></div><d=
iv><pre style=3D"background-color:#ffffff;color:#000000;font-family:&#39;De=
jaVu Sans Mono&#39;;font-size:11.3pt"><span style=3D"color:rgb(0,0,255)"><s=
pan style=3D"font-weight:bold">class </span>A {<br> <span style=3D"font-wei=
ght:bold">public</span>:<br>  <span style=3D"font-weight:bold">virtual void=
 </span>func() {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold"=
>&quot;A::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span=
 style=3D"font-weight:bold">class </span>B : <span style=3D"font-weight:bol=
d">public </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br=
>  <span style=3D"font-weight:bold">void </span>func() override {<br>    st=
d::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;B::func()&quot; </s=
pan>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold=
">class </span>C : <span style=3D"font-weight:bold">public </span>A {<br> <=
span style=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-wei=
ght:bold">void </span>func() override {<br>    std::cout &lt;&lt; <span sty=
le=3D"font-weight:bold">&quot;C::func()&quot; </span>&lt;&lt; std::endl;<br=
>  }<br>};<br><br><span style=3D"font-weight:bold">class </span>D : <span s=
tyle=3D"font-weight:bold">public </span>B, <span style=3D"font-weight:bold"=
>public </span>C {<br> <span style=3D"font-weight:bold">public</span>:<br> =
 <span style=3D"font-weight:bold">void </span>func() override {<br>    std:=
:cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::func()&quot; </spa=
n>&lt;&lt; std::endl;<br>  }<br>};</span><br><br>For given class D it is no=
t possible to override <b>func</b> only for specific class <span style=3D"c=
olor:#000080;font-weight:bold"></span><span style=3D"color:#008080">B:<br><=
br></span><span style=3D"color:rgb(0,0,255)">D d;<br>B bb;<br>B &amp; b =3D=
 d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c;<br>aB.fun=
c();<br>b.func();<br>aC.func();<br>c.func();</span><br><br><b>Prints:</b><b=
r><span style=3D"color:rgb(0,0,255)">D::func()<br>D::func()<br>D::func()<br=
>D::func()</span><br><b><font size=3D"4"><br><br>The Solution:</font><br><b=
r></b>Adding the special kind of syntax it is possible to override <b>func<=
/b> only for <b>B</b> or <b>C</b> classes<br><br><span style=3D"color:rgb(0=
,0,255)"><span style=3D"font-weight:bold">class </span>A {<br> <span style=
=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weight:bold">=
virtual void </span>func() {<br>    std::cout &lt;&lt; <span style=3D"font-=
weight:bold">&quot;A::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<=
br><br><span style=3D"font-weight:bold">class </span>B : <span style=3D"fon=
t-weight:bold">public </span>A {<br> <span style=3D"font-weight:bold">publi=
c</span>:<br>  <span style=3D"font-weight:bold">void </span>func() override=
 {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;B::func=
()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font=
-weight:bold">class </span>C : <span style=3D"font-weight:bold">public </sp=
an>A {<br> <span style=3D"font-weight:bold">public</span>:<br>  <span style=
=3D"font-weight:bold">void </span>func() override {<br>    std::cout &lt;&l=
t; <span style=3D"font-weight:bold">&quot;C::func()&quot; </span>&lt;&lt; s=
td::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">class </span=
>D : <span style=3D"font-weight:bold">public </span>B, <span style=3D"font-=
weight:bold">public </span>C {<br> <span style=3D"font-weight:bold">public<=
/span>:<br>  <span style=3D"font-weight:bold">void B::</span>func() overrid=
e {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::B::=
</span><span style=3D"font-weight:bold"><span style=3D"font-weight:bold"></=
span>func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>  <span style=3D"fon=
t-weight:bold">void </span>func() override {<br>    std::cout &lt;&lt; <spa=
n style=3D"font-weight:bold">&quot;D::func()&quot; </span>&lt;&lt; std::end=
l;<br>  }<br>};</span><br><br><span style=3D"color:rgb(0,0,255)">D d;<br>B =
&amp; b =3D d;<br>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c=
;<br>aB.func();<br>b.func();<br>aC.func();<br>c.func();</span><br><br><b>Pr=
ints:</b><br><span style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:=
bold">D::B::</span><span style=3D"font-weight:bold"><span style=3D"font-wei=
ght:bold"></span>func()</span><br><span style=3D"font-weight:bold">D::B::</=
span><span style=3D"font-weight:bold"><span style=3D"font-weight:bold"></sp=
an>func()</span><br><span style=3D"font-weight:bold">D::func()</span><br><s=
pan style=3D"font-weight:bold">D::func()</span></span><br><br>or for exampl=
e to do something like this:<br><br><span style=3D"color:rgb(0,0,255)"><spa=
n style=3D"font-weight:bold">class </span>A {<br> <span style=3D"font-weigh=
t:bold">public</span>:<br>  <span style=3D"font-weight:bold">virtual void <=
/span>func() {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&=
quot;A::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span s=
tyle=3D"font-weight:bold">class </span>B : <span style=3D"font-weight:bold"=
>public </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br> =
 <span style=3D"font-weight:bold">void </span>func() override {<br>    std:=
:cout &lt;&lt; <span style=3D"font-weight:bold">&quot;B::func()&quot; </spa=
n>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">=
class </span>C : <span style=3D"font-weight:bold">public </span>A {<br> <sp=
an style=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weigh=
t:bold">void </span>func() override {<br>    std::cout &lt;&lt; <span style=
=3D"font-weight:bold">&quot;C::func()&quot; </span>&lt;&lt; std::endl;<br> =
 }<br>};<br><br><span style=3D"font-weight:bold">class </span>D : <span sty=
le=3D"font-weight:bold">public </span>B, <span style=3D"font-weight:bold">p=
ublic </span>C {<br> <span style=3D"font-weight:bold">public</span>:<br>  <=
span style=3D"font-weight:bold">void B::</span>func() override {<br>    std=
::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::B::</span><span s=
tyle=3D"font-weight:bold"><span style=3D"font-weight:bold"></span>func()&qu=
ot; </span>&lt;&lt; std::endl;<br>  }<br>  <span style=3D"font-weight:bold"=
>void </span><span style=3D"font-weight:bold"></span>C::func() override {<b=
r>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;D::C::func(=
)&quot; </span>&lt;&lt; std::endl;<br>  }<br>};</span></pre></div></div></b=
lockquote><div><br></div><div><h1 style=3D"font-stretch:normal;line-height:=
1.2;background-image:initial;background-position:initial;background-repeat:=
initial"><font style=3D"font-family:sans-serif" size=3D"2"><font color=3D"#=
000000">p0945 &quot;</font>Generalizing alias declarations&quot; provides a=
 more general syntax that covers this use case among others. See the final =
example under <a href=3D"http://wg21.link/p0945#" target=3D"_blank" rel=3D"=
nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dht=
tp%3A%2F%2Fwg21.link%2Fp0945%23\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHau=
Me0YVSPg34Q5jHfLoRMJwneLQ&#39;;return true;" onclick=3D"this.href=3D&#39;ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fwg21.link%2Fp0945%23\x26sa\x3dD\x=
26sntz\x3d1\x26usg\x3dAFQjCNHauMe0YVSPg34Q5jHfLoRMJwneLQ&#39;;return true;"=
>http://wg21.link/p0945#</a></font><font size=3D"2" face=3D"sans-serif">mem=
ber-<wbr>aliases .</font></h1></div><div>=C2=A0</div><blockquote class=3D"g=
mail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;=
padding-left:1ex"><div dir=3D"ltr"><div><pre style=3D"background-color:#fff=
fff;color:#000000;font-family:&#39;DejaVu Sans Mono&#39;;font-size:11.3pt">=
<span style=3D"color:rgb(0,0,255)">D d;<br>B &amp; b =3D d;<br>A &amp; aB =
=3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c;<br>aB.func();<br>b.func();<=
br>aC.func();<br>c.func();<br>d.func();</span> <b>// <span style=3D"color:r=
gb(255,0,0)">Error. Ambiguity !!</span></b><br><br><b>Prints:</b><br><span =
style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold">D::B::</span>=
<span style=3D"font-weight:bold"><span style=3D"font-weight:bold"></span>fu=
nc()</span><span style=3D"font-weight:bold"></span><br><span style=3D"font-=
weight:bold">D::B::</span><span style=3D"font-weight:bold"><span style=3D"f=
ont-weight:bold"></span>func()</span><span style=3D"font-weight:bold"></spa=
n><br><span style=3D"font-weight:bold">D::C::func()</span><br></span><span =
style=3D"color:#008000;font-weight:bold"><span style=3D"color:rgb(0,0,255)"=
><span style=3D"font-weight:bold">D::C::func()</span></span><br><br></span>=
<span style=3D"color:rgb(0,128,0)"><span style=3D"color:rgb(0,0,0)">To fix =
the error you should do something like this:</span></span><span style=3D"co=
lor:#008000;font-weight:bold"><br></span><br><span style=3D"color:rgb(0,0,2=
55)"><span style=3D"font-weight:bold"><span style=3D"font-weight:bold">clas=
s </span>A {<br> <span style=3D"font-weight:bold">public</span>:<br>  <span=
 style=3D"font-weight:bold">virtual void </span>func() {<br>    std::cout &=
lt;&lt; <span style=3D"font-weight:bold">&quot;A::func()&quot; </span>&lt;&=
lt; std::endl;<br>  }<br>};<br><br><span style=3D"font-weight:bold">class <=
/span>B : <span style=3D"font-weight:bold">public </span>A {<br> <span styl=
e=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weight:bold"=
>void </span>func() override {<br>    std::cout &lt;&lt; <span style=3D"fon=
t-weight:bold">&quot;B::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br>}=
;<br><br><span style=3D"font-weight:bold">class </span>C : <span style=3D"f=
ont-weight:bold">public </span>A {<br> <span style=3D"font-weight:bold">pub=
lic</span>:<br>  <span style=3D"font-weight:bold">void </span>func() overri=
de {<br>    std::cout &lt;&lt; <span style=3D"font-weight:bold">&quot;C::fu=
nc()&quot; </span>&lt;&lt; std::endl;<br>  }<br>};<br><br><span style=3D"fo=
nt-weight:bold">class </span>D : <span style=3D"font-weight:bold">public </=
span>B, <span style=3D"font-weight:bold">public </span>C {<br> <span style=
=3D"font-weight:bold">public</span>:<br>  <span style=3D"font-weight:bold">=
void B::</span>func() override {<br>    std::cout &lt;&lt; <span style=3D"f=
ont-weight:bold">&quot;D::B::</span><span style=3D"font-weight:bold"><span =
style=3D"font-weight:bold"></span>func()&quot; </span>&lt;&lt; std::endl;<b=
r>  }<br>  <span style=3D"font-weight:bold">void </span><span style=3D"font=
-weight:bold"></span>C::func() override {<br>    std::cout &lt;&lt; <span s=
tyle=3D"font-weight:bold">&quot;D::C::func()&quot; </span>&lt;&lt; std::end=
l;<br>  }</span><br></span><span style=3D"color:#008000;font-weight:bold"><=
span style=3D"color:rgb(0,0,255)"><span style=3D"font-weight:bold">  <span =
style=3D"font-weight:bold">void </span><span style=3D"font-weight:bold"></s=
pan>func() override {<br>    std::cout &lt;&lt; <span style=3D"font-weight:=
bold">&quot;D::func()&quot; </span>&lt;&lt; std::endl;<br>  }<br></span>};<=
/span><br><br><span style=3D"color:rgb(0,0,255)">D d;<br>B &amp; b =3D d;<b=
r>A &amp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c;<br>aB.func();<=
br>b.func();<br>aC.func();<br>c.func();<br>d.func();</span>  // Ok<br><br><=
span style=3D"color:rgb(0,0,0)"><b>Prints:</b></span><br><span style=3D"col=
or:rgb(0,0,255)"><span style=3D"font-weight:bold">D::B::</span><span style=
=3D"font-weight:bold"><span style=3D"font-weight:bold"></span>func()</span>=
<span style=3D"font-weight:bold"></span><br><span style=3D"font-weight:bold=
">D::B::</span><span style=3D"font-weight:bold"><span style=3D"font-weight:=
bold"></span>func()</span><span style=3D"font-weight:bold"></span><br><span=
 style=3D"font-weight:bold">D::C::func()</span><br><span style=3D"font-weig=
ht:bold"><span style=3D"font-weight:bold">D::C::func()<br></span></span></s=
pan></span><span style=3D"color:#008000;font-weight:bold"><span style=3D"co=
lor:#008000;font-weight:bold"><span style=3D"color:rgb(0,0,0)"><span style=
=3D"color:#008000;font-weight:bold"><span style=3D"color:rgb(0,0,255)">D::f=
unc()</span><br><br><br></span></span></span></span><span style=3D"color:rg=
b(0,128,0)"><span style=3D"color:rgb(0,128,0)"><span style=3D"color:rgb(0,0=
,0)"><span style=3D"color:rgb(0,128,0)"><span style=3D"color:rgb(0,0,0)">I =
have already implemented something similar without new syntax be hacking th=
e existing object structure:</span></span></span></span></span><span style=
=3D"color:#008000;font-weight:bold"><span style=3D"color:#008000;font-weigh=
t:bold"><span style=3D"color:rgb(0,0,0)"><span style=3D"color:#008000;font-=
weight:bold"><br></span></span></span></span><br><span style=3D"color:rgb(0=
,0,255)"><span style=3D"font-weight:bold"><span style=3D"font-weight:bold">=
<span style=3D"font-weight:bold">D d;<br>uint8_t * dData =3D <span style=3D=
"font-weight:bold">reinterpret_cast</span>&lt;uint8_t *&gt;(&amp;d);<br>B b=
b;<br>uint8_t * bbData =3D <span style=3D"font-weight:bold">reinterpret_cas=
t</span>&lt;uint8_t *&gt;(&amp;bb);<br>dData[0] =3D bbData[0];<br>dData[1] =
=3D bbData[1];<br>dData[2] =3D bbData[2];<br>dData[3] =3D bbData[3];<br>dDa=
ta[4] =3D bbData[4];<br>dData[5] =3D bbData[5];<br>B &amp; b =3D d;<br>A &a=
mp; aB =3D b;<br>C &amp; c =3D d;<br>A &amp; aC =3D c;<br>aB.func();<br>b.f=
unc();<br>aC.func();<br>c.func();<br></span></span></span><br><span style=
=3D"color:rgb(0,0,0)">What do you think ?</span></span></pre></div></div></=
blockquote><div><br></div></div></blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/0415ffeb-e4ea-4e2f-9f55-03d9110ddfd1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0415ffeb-e4ea-4e2f-9f55-03d9110ddfd1=
%40isocpp.org</a>.<br />

------=_Part_2028_1125004924.1537254426139--

------=_Part_2027_92558354.1537254426138--

.