Topic: [proposal] using keyword "using" to help create proxy classes.


Author: Victor Bogado <bogado@gmail.com>
Date: Sat, 22 Oct 2016 12:42:20 -0700 (PDT)
Raw View
------=_Part_2450_814555610.1477165340514
Content-Type: multipart/alternative;
 boundary="----=_Part_2451_818677411.1477165340514"

------=_Part_2451_818677411.1477165340514
Content-Type: text/plain; charset=UTF-8

Hi,

First I'm sorry the spam the web interface sent my post before it was
finished. >:-(

I'm a software engineer that loves C++. I work mostly with Android, that
means that my day job is mostly java, unfortunately and I pursue C++ mostly
on my free time. I just thought an idea that would make it easier and
cleaner to create proxy classes using the 'using' keyword as a shorthand to
create proxy methods and members in general.

The basic idea is just that use the 'using' keyword to reference another
member and define members in the same way it can be used to define types
based on other dependent types. The sample code bellow shows a generic
pimpl implementation and next I use the using keyword to simplify and make
the code more readable.

---------------------------------------- C++14/17
----------------------------------------

template <typename T>
class pimpl {
    T impl;
public:
    template <typename ArgTypes...>
    pimpl(ArgTypes&&... args) :
        impl(std::forward<ArgTypes>(args)...)
    {}

    template <typename ArgTypes...>
    void method1(ArgTtypes&&... args) {
        impl.method1(std::forward<ArgTypes>(args)...);
    }

    // continue for each method on "T"
};

---------------------------------------- basic proposal
----------------------------------------

template <typename T>
class pimpl {
    T impl;
public:
    // Using the name of the class forwards the constructors.
    using pimpl = impl;

    // This is the equivalent of the definition of method1 above.
    using method1 = impl.method1;
};

---------------------------------------- extended proposal
----------------------------------------

template <typename T>
class pimpl {
    std::unique_ptr<T> impl;
public:

    // makes method1(...) work as impl->method1(...)
    using method1 = impl->method1;

    // go crazy?
    using method2 = impl->a->b().what;

    // generic forwarding, forwards all non-defined methods
    // option 1:
    using *= impl->*;
    // option 2: more verbose, less cryptic (maybe).
    using auto = impl->auto;

    // possible constructor syntax?
    using pimpl = make_unique<T> -> impl;

    // or just as usual :
    template <ArgTypes...>
    pimpl(ArgTypes&&... args) :
        impl(std::forward<ArgTypes>...)
    {}
};

I'm not sure how hard would it be to create this, but the syntax would not
be ambiguous because the left hand side of the using operation would
translate to either a method of some field/member or an expression
containing "...". This would basically be syntactic sugar to a vardiactic
template that forwards all arguments to the "..." site or to the parameter
list of the result of the operation.

So if the right side of the using is a type the normal using is used.
Otherwise the expression is used to define a method that forwards the
arguments.

Victor Bogado da Silva Lins

--
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/e765ead7-16bb-4f10-9c68-51b49f52c01f%40isocpp.org.

------=_Part_2451_818677411.1477165340514
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<br><br>First I&#39;m sorry the spam the web interface =
sent my post before it was finished. &gt;:-(<br><br>I&#39;m a software engi=
neer that loves C++. I work mostly with Android, that means that my day job=
 is mostly java, unfortunately and I pursue C++ mostly on my free time. I j=
ust thought an idea that would make it easier and cleaner to create proxy c=
lasses using the &#39;using&#39; keyword as a shorthand to create proxy met=
hods and members in general.<br><br>The basic idea is just that use the &#3=
9;using&#39; keyword to reference another member and define members in the =
same way it can be used to define types based on other dependent types. The=
 sample code bellow shows a generic pimpl implementation and next I use the=
 using keyword to simplify and make the code more readable.<br><br>--------=
-------------------------------- C++14/17 ---------------------------------=
-------<br><br>template &lt;typename T&gt;<br>class pimpl {<br>=C2=A0=C2=A0=
=C2=A0 T impl;<br>public:<br>=C2=A0=C2=A0=C2=A0 template &lt;typename ArgTy=
pes...&gt;<br>=C2=A0=C2=A0=C2=A0 pimpl(ArgTypes&amp;&amp;... args) :<br>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 impl(std::forward&lt;ArgTypes&gt;(a=
rgs)...)<br>=C2=A0=C2=A0=C2=A0 {}<br><br>=C2=A0=C2=A0=C2=A0 template &lt;ty=
pename ArgTypes...&gt;<br>=C2=A0=C2=A0=C2=A0 void method1(ArgTtypes&amp;&am=
p;... args) {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 impl.method1(st=
d::forward&lt;ArgTypes&gt;(args)...);<br>=C2=A0=C2=A0=C2=A0 }<br><br>=C2=A0=
=C2=A0=C2=A0 // continue for each method on &quot;T&quot;<br>};<br><br>----=
------------------------------------ basic proposal -----------------------=
-----------------<br><br>template &lt;typename T&gt;<br>class pimpl {<br>=
=C2=A0=C2=A0=C2=A0 T impl;<br>public:<br>=C2=A0=C2=A0=C2=A0 // Using the na=
me of the class forwards the constructors.<br>=C2=A0=C2=A0=C2=A0 using pimp=
l =3D impl;<br><br>=C2=A0=C2=A0=C2=A0 // This is the equivalent of the defi=
nition of method1 above.<br>=C2=A0=C2=A0=C2=A0 using method1 =3D impl.metho=
d1;<br>};<br><br>---------------------------------------- extended proposal=
 ----------------------------------------<br><br>template &lt;typename T&gt=
;<br>class pimpl {<br>=C2=A0=C2=A0=C2=A0 std::unique_ptr&lt;T&gt; impl;<br>=
public:<br><br>=C2=A0=C2=A0=C2=A0 // makes method1(...) work as impl-&gt;me=
thod1(...)<br>=C2=A0=C2=A0=C2=A0 using method1 =3D impl-&gt;method1;<br><br=
>=C2=A0=C2=A0=C2=A0 // go crazy?<br>=C2=A0=C2=A0=C2=A0 using method2 =3D im=
pl-&gt;a-&gt;b().what;<br><br>=C2=A0=C2=A0=C2=A0 // generic forwarding, for=
wards all non-defined methods<br>=C2=A0=C2=A0=C2=A0 // option 1:<br>=C2=A0=
=C2=A0=C2=A0 using *=3D impl-&gt;*;<br>=C2=A0=C2=A0=C2=A0 // option 2: more=
 verbose, less cryptic (maybe).<br>=C2=A0=C2=A0=C2=A0 using auto =3D impl-&=
gt;auto;<br><br>=C2=A0=C2=A0=C2=A0 // possible constructor syntax?<br>=C2=
=A0=C2=A0=C2=A0 using pimpl =3D make_unique&lt;T&gt; -&gt; impl;<br><br>=C2=
=A0=C2=A0=C2=A0 // or just as usual :<br>=C2=A0=C2=A0=C2=A0 template &lt;Ar=
gTypes...&gt;<br>=C2=A0=C2=A0=C2=A0 pimpl(ArgTypes&amp;&amp;... args) :<br>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 impl(std::forward&lt;ArgTypes&gt=
;...)<br>=C2=A0=C2=A0=C2=A0 {}<br>};<br><br>I&#39;m not sure how hard would=
 it be to create this, but the syntax would not be ambiguous because the le=
ft hand side of the using operation would translate to either a method of s=
ome field/member or an expression containing &quot;...&quot;. This would ba=
sically be syntactic sugar to a vardiactic template that forwards all argum=
ents to the &quot;...&quot; site or to the parameter list of the result of =
the operation.<br><br>So if the right side of the using is a type the norma=
l using is used. Otherwise the expression is used to define a method that f=
orwards the arguments.<br><br>Victor Bogado da Silva Lins<br></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/e765ead7-16bb-4f10-9c68-51b49f52c01f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e765ead7-16bb-4f10-9c68-51b49f52c01f=
%40isocpp.org</a>.<br />

------=_Part_2451_818677411.1477165340514--

------=_Part_2450_814555610.1477165340514--

.