Topic: Layers: inlining definitions (was Statement


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Tue, 7 Feb 2017 22:18:26 +0100
Raw View
This is a multi-part message in MIME format.
--------------47126C1225E02C94F129D8AC
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 07/02/2017 =C3=A0 15:52, Ville Voutilainen a =C3=A9crit :
> On 7 February 2017 at 16:50, Nicol Bolas <jmckesson@gmail.com> wrote:
>>> More generally I wonder if extending inline functions with something mi=
ght
>>> be a nicer route:
>> The point of a statement expression is that it's part of the current
>> function. It's not doing a separate call; it's just a block of code that
>> produces a value. That way, you can still do things like access local
>> variables and parameters, use `break` and `return` as if you were in the
>> function (which you are) etc.
> Here's a question: how do I get from this proposal to a
> statement-expression template?
> I want to write generic code blocks that I can glue into other places,
> passing template
> arguments that guide how the code is instantiated.
>
Glad to hear that some are for other kind of composition.

Let me share an idea I have in mind.

I want to write a generic set of definitions that I can glue into=20
several classes.

I will name this construction a layer (other name this mixins). A layer=20
consist of a set of declarations we can find inside a class definition=20
and that can be inlined in a specific class as if the declarations were=20
written by hand.

The first use case I have in mind is the definition of the comparison=20
operators that are defined in function of another operation

namespace layer {

*using* strong_ordered : *inline* Type

{
     friend operator=3D=3D(const Type& x, const Type& y) { return (x <=3D> =
y)=20
=3D=3D 0; } //<=3D> is a 3way comparison operator as proposed in  D0515 -=
=20
Consistent comparison
     friend operator!=3D (const Type& x, const Type& y) { return (x <=3D> y=
)=20
!=3D 0; }
     friend operator<  (const Type& x, const Type& y) { return (x <=3D> y)=
=20
< 0; }
     friend operator<=3D(const Type& x, const Type& y) { return (x <=3D> y)=
=20
<=3D 0; }
     friend operator>  (const Type& x, const Type& y) { return (x <=3D> y)=
=20
 > 0; }
     friend operator>=3D(const Type& x, const Type& y) { return (x <=3D> y)=
=20
 >=3D 0; }
};
}


class Point {
     int x;
     int y;
public:

     friend strong_ordering operator<=3D>(const Point&, const Point&)=20
{.....} // D0515 - Consistent comparison

*    inline layer::derive_strong_ordering; // derive strong ordering=20
operations for Point*

     // ... other functions, but no other user-declared comparisons ...
};

I believe that this kind of layers could avoid the need for *some*=20
additional =3Ddefault functions, e.g. default comparisons, default swap,=20
default hash, ....


I know all this can be done using macros, but I guess that using a=20
language construct could allow to add some constraints between the=20
layers and the scoping type, e.g. we could have requirements on the=20
scoping type Type

*using* strong_ordered : *inline* Type
*requires ***(Type a,  Type b) *  {a<=3D>b} -> **strong_ordering*

*{...};
*

We could have of course layer templates and layers that nest other layers.

Another use cases could be the definition of opaque types. The layers=20
could be used in order to define the operations that are built on top of=20
the underlying type. Inlining those layers would help to the definition=20
of opaque types.

I know that we can implement this kind of abstractions using CRTP. But=20
we want to be able to do simple things simple.

This is in some way related to mixins, that have been discussed already=20
on this ML.

There was also a related thread in this ML (A strong typedef syntax=20
idea) about duplicating classes.

There are surely a lot issues that I've not yet think about (use of an=20
incomple type) while inlining, ...).

Comments welcome.


Vicente



--=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/0f9df0e1-72e2-b26d-4625-a36529760d6f%40wanadoo.f=
r.

--------------47126C1225E02C94F129D8AC
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>

    <meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-8=
">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Le 07/02/2017 =C3=A0 15:52, Ville
      Voutilainen a =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote
cite=3D"mid:CAFk2RUYgvAUNpM3JjQ-h6wLt_VtXLzmoVMOrzYoKj6C+KjENuA@mail.gmail.=
com"
      type=3D"cite">
      <pre wrap=3D"">On 7 February 2017 at 16:50, Nicol Bolas <a class=3D"m=
oz-txt-link-rfc2396E" href=3D"mailto:jmckesson@gmail.com">&lt;jmckesson@gma=
il.com&gt;</a> wrote:
</pre>
      <blockquote type=3D"cite">
        <blockquote type=3D"cite">
          <pre wrap=3D"">More generally I wonder if extending inline functi=
ons with something might
be a nicer route:
</pre>
        </blockquote>
        <pre wrap=3D"">
The point of a statement expression is that it's part of the current
function. It's not doing a separate call; it's just a block of code that
produces a value. That way, you can still do things like access local
variables and parameters, use `break` and `return` as if you were in the
function (which you are) etc.
</pre>
      </blockquote>
      <pre wrap=3D"">
Here's a question: how do I get from this proposal to a
statement-expression template?
I want to write generic code blocks that I can glue into other places,
passing template
arguments that guide how the code is instantiated.

</pre>
    </blockquote>
    <p>Glad to hear that some are for other kind of composition.<br>
    </p>
    <p>Let me share an idea I have in mind.</p>
    <p>I want to write a generic set of definitions that I can glue into
      several classes. <br>
    </p>
    <p>I will name this construction a layer (other name this mixins). A
      layer consist of a set of declarations we can find inside a class
      definition and that can be inlined in a specific class as if the
      declarations were written by hand.<br>
    </p>
    <p>The first use case I have in mind is the definition of the
      comparison operators that are defined in function of another
      operation<br>
    </p>
    <p> namespace layer {<br>
      <br>
      <b>using</b> strong_ordered : <b>inline</b> Type <br>
    </p>
    <p>{ <br>
      =C2=A0=C2=A0=C2=A0 friend operator=3D=3D(const Type&amp; x, const Typ=
e&amp; y) {
      return (x &lt;=3D&gt; y) =3D=3D 0; } //&lt;=3D&gt; is a 3way comparis=
on
      operator as proposed in=C2=A0 D0515 - Consistent comparison<br>
      =C2=A0=C2=A0=C2=A0 friend operator!=3D (const Type&amp; x, const Type=
&amp; y) {
      return (x &lt;=3D&gt; y) !=3D 0; }<br>
      =C2=A0=C2=A0=C2=A0 friend operator&lt;=C2=A0 (const Type&amp; x, cons=
t Type&amp; y) {
      return (x &lt;=3D&gt; y) &lt; 0; }<br>
      =C2=A0=C2=A0=C2=A0 friend operator&lt;=3D(const Type&amp; x, const Ty=
pe&amp; y) {
      return (x &lt;=3D&gt; y) &lt;=3D 0; }<br>
      =C2=A0=C2=A0=C2=A0 friend operator&gt;=C2=A0 (const Type&amp; x, cons=
t Type&amp; y) {
      return (x &lt;=3D&gt; y) &gt; 0; }<br>
      =C2=A0=C2=A0=C2=A0 friend operator&gt;=3D(const Type&amp; x, const Ty=
pe&amp; y) {
      return (x &lt;=3D&gt; y) &gt;=3D 0; }<br>
      };<br>
      }</p>
    <p><br>
    </p>
    <p> class Point { <br>
      =C2=A0=C2=A0=C2=A0 int x; <br>
      =C2=A0=C2=A0=C2=A0 int y; <br>
      public: <br>
    </p>
    <p> =C2=A0=C2=A0=C2=A0 friend strong_ordering operator&lt;=3D&gt;(const=
 Point&amp;,
      const Point&amp;) {.....} // D0515 - Consistent comparison
    </p>
    <p> <b>=C2=A0=C2=A0=C2=A0 inline layer::derive_strong_ordering; // deri=
ve strong
        ordering operations for Point</b><br>
      =C2=A0=C2=A0=C2=A0 <br>
      =C2=A0=C2=A0=C2=A0 // ... other functions, but no other user-declared=
 comparisons
      ... <br>
      };</p>
    <p>I believe that this kind of layers could avoid the need for
      *some* additional =3Ddefault functions, e.g. default comparisons,
      default swap, default hash, ....<br>
    </p>
    <p><br>
    </p>
    <p>I know all this can be done using macros, but I guess that using
      a language construct could allow to add some constraints between
      the layers and the scoping type, e.g. we could have requirements
      on the scoping type Type<br>
    </p>
    <p> <b>using</b> strong_ordered : <b>inline</b> Type <br>
      <b>requires </b><b><b>(Type a,=C2=A0 Type b)=C2=A0</b>=C2=A0 {a&lt;=
=3D&gt;b} -&gt;
      </b><b>strong_ordering</b></p>
    <p><b>{...};<br>
      </b></p>
    We could have of course layer templates and layers that nest other
    layers.
    <p>Another use cases could be the definition of opaque types. The
      layers could be used in order to define the operations that are
      built on top of the underlying type. Inlining those layers would
      help to the definition of opaque types.<br>
    </p>
    <p>I know that we can implement this kind of abstractions using
      CRTP. But we want to be able to do simple things simple.</p>
    <p>This is in some way related to mixins, that have been discussed
      already on this ML. <br>
    </p>
    <p>There was also a related thread in this ML (A strong typedef
      syntax idea) about duplicating classes.<br>
    </p>
    <p>There are surely a lot issues that I've not yet think about (use
      of an incomple type) while inlining, ...).</p>
    <p>Comments welcome.<br>
    </p>
    <p><br>
    </p>
    <p>Vicente</p>
    <p><br>
    </p>
    <p><br>
    </p>
  </body>
</html>

<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/0f9df0e1-72e2-b26d-4625-a36529760d6f%=
40wanadoo.fr?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0f9df0e1-72e2-b26d-4625-a36529760d6f=
%40wanadoo.fr</a>.<br />

--------------47126C1225E02C94F129D8AC--

.