Topic: Question about concepts and forwarding references


Author: Kazutoshi Satoda <k_satoda@f2.dion.ne.jp>
Date: Thu, 27 Sep 2018 02:54:35 +0900
Raw View
Reading the latest draft N4762 and http://wg21.link/P1141R0 (YAACD),
I got worried about possible annoyance in use of concepts with
forwarding references.

Consider the following example <https://wandbox.org/permlink/mn9eCK2zdCrdEFMH>:

  #include <type_traits>

  template<typename T> requires std::is_integral_v<T>
  void f(T&&) {}
  // http://wg21.link/P1141R0 (YAACD) will shorten this as:
  //   void f(std::Integral auto&&) {}

  int main()
  {
    int i = 0;
    f(i); // causes an error because T is int&, not int.
  }

I can fix the verbose version by adding std::remove_reference_t, but I
can't figure out how to fix the shortened version without being verbose.
If it needs to be verbose and needs remove_reference_t or something, I
think it will be somewhat annoying in using or defining concepts.

Am I missing something?

--
k_satoda

--
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/3bde66be-03eb-d6fd-242d-d6d99fde3b22%40f2.dion.ne.jp.

.


Author: Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
Date: Wed, 26 Sep 2018 17:25:04 -0700 (PDT)
Raw View
------=_Part_40_401369106.1538007905026
Content-Type: multipart/alternative;
 boundary="----=_Part_41_2071092346.1538007905027"

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

On Wednesday, September 26, 2018 at 10:54:49 AM UTC-7, Kazutoshi SATODA=20
wrote:
>
> Reading the latest draft N4762 and http://wg21.link/P1141R0 (YAACD),=20
> I got worried about possible annoyance in use of concepts with=20
> forwarding references.=20
>
> Consider the following example <
> https://wandbox.org/permlink/mn9eCK2zdCrdEFMH>:=20
>
>   #include <type_traits>=20
>
>   template<typename T> requires std::is_integral_v<T>=20
>   void f(T&&) {}=20
>   // http://wg21.link/P1141R0 (YAACD) will shorten this as:=20
>   //   void f(std::Integral auto&&) {}=20
>
>   int main()=20
>   {=20
>     int i =3D 0;=20
>     f(i); // causes an error because T is int&, not int.=20
>   }=20
>
> I can fix the verbose version by adding std::remove_reference_t, but I=20
> can't figure out how to fix the shortened version without being verbose.=
=20
> If it needs to be verbose and needs remove_reference_t or something, I=20
> think it will be somewhat annoying in using or defining concepts.=20
>
> Am I missing something?=20
>

Nope, this is a real problem.  Here's my contribution to the literature:
https://quuxplusone.github.io/blog/2018/09/09/constrained-forwarding-refere=
nces/
 (reddit=20
<https://www.reddit.com/r/cpp/comments/9ekxay/constrained_forwarding_refere=
nces_considered/>
)
and I know Casey Carter (and presumably others) were already aware of it=20
before I was.

I am not aware of any active work specifically on *fixing* the problem,=20
though. Maybe someone else knows?

My own observation/hope is that maybe we can develop idioms and conventions=
=20
for concepts, e.g.,
- We say a concept C is *fooish* if C<T> implies C<const T&> and vice=20
versa. Every concept you write should be *fooish*.
- When using the terser syntax, never take a concept-constrained parameter=
=20
by anything except lvalue reference.
- Never take a concept-constrained parameter by anything except lvalue=20
reference or by-value (for *fooish* concepts).

If we can get a concise and teachable set of rules for concepts, then IMO=
=20
it's okay if you can still hit surprising bugs (only) by breaking those=20
rules.

I imagine that the Ranges people must have some kind of informal mental=20
model that they used in writing Ranges' concepts, even if it hasn't been=20
formally written down yet.

=E2=80=93Arthur

--=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/96a22750-e9eb-4c08-a5d1-2d0f4d030183%40isocpp.or=
g.

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

<div dir=3D"ltr">On Wednesday, September 26, 2018 at 10:54:49 AM UTC-7, Kaz=
utoshi SATODA wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Reading th=
e latest draft N4762 and <a href=3D"http://wg21.link/P1141R0" target=3D"_bl=
ank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.com=
/url?q\x3dhttp%3A%2F%2Fwg21.link%2FP1141R0\x26sa\x3dD\x26sntz\x3d1\x26usg\x=
3dAFQjCNGwmgaQvtV_vax-mHotwThfxJRewg&#39;;return true;" onclick=3D"this.hre=
f=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwg21.link%2FP1141R0\x2=
6sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGwmgaQvtV_vax-mHotwThfxJRewg&#39;;ret=
urn true;">http://wg21.link/P1141R0</a> (YAACD),
<br>I got worried about possible annoyance in use of concepts with
<br>forwarding references.
<br>
<br>Consider the following example &lt;<a href=3D"https://wandbox.org/perml=
ink/mn9eCK2zdCrdEFMH" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"thi=
s.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwandbox.org%2Fp=
ermlink%2Fmn9eCK2zdCrdEFMH\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJJQb8e-=
T68wDA0gC_QM9f1OVLuQ&#39;;return true;" onclick=3D"this.href=3D&#39;https:/=
/www.google.com/url?q\x3dhttps%3A%2F%2Fwandbox.org%2Fpermlink%2Fmn9eCK2zdCr=
dEFMH\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJJQb8e-T68wDA0gC_QM9f1OVLuQ&=
#39;;return true;">https://wandbox.org/permlink/<wbr>mn9eCK2zdCrdEFMH</a>&g=
t;:
<br>
<br>=C2=A0 #include &lt;type_traits&gt;
<br>
<br>=C2=A0 template&lt;typename T&gt; requires std::is_integral_v&lt;T&gt;
<br>=C2=A0 void f(T&amp;&amp;) {}
<br>=C2=A0 // <a href=3D"http://wg21.link/P1141R0" target=3D"_blank" rel=3D=
"nofollow" onmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dh=
ttp%3A%2F%2Fwg21.link%2FP1141R0\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGwm=
gaQvtV_vax-mHotwThfxJRewg&#39;;return true;" onclick=3D"this.href=3D&#39;ht=
tp://www.google.com/url?q\x3dhttp%3A%2F%2Fwg21.link%2FP1141R0\x26sa\x3dD\x2=
6sntz\x3d1\x26usg\x3dAFQjCNGwmgaQvtV_vax-mHotwThfxJRewg&#39;;return true;">=
http://wg21.link/P1141R0</a> (YAACD) will shorten this as:
<br>=C2=A0 // =C2=A0 void f(std::Integral auto&amp;&amp;) {}
<br>
<br>=C2=A0 int main()
<br>=C2=A0 {
<br>=C2=A0 =C2=A0 int i =3D 0;
<br>=C2=A0 =C2=A0 f(i); // causes an error because T is int&amp;, not int.
<br>=C2=A0 }
<br>
<br>I can fix the verbose version by adding std::remove_reference_t, but I
<br>can&#39;t figure out how to fix the shortened version without being ver=
bose.
<br>If it needs to be verbose and needs remove_reference_t or something, I
<br>think it will be somewhat annoying in using or defining concepts.
<br>
<br>Am I missing something?
<br></blockquote><div><br></div><div>Nope, this is a real problem. =C2=A0He=
re&#39;s my contribution to the literature:</div><div><a href=3D"https://qu=
uxplusone.github.io/blog/2018/09/09/constrained-forwarding-references/">htt=
ps://quuxplusone.github.io/blog/2018/09/09/constrained-forwarding-reference=
s/</a>=C2=A0(<a href=3D"https://www.reddit.com/r/cpp/comments/9ekxay/constr=
ained_forwarding_references_considered/">reddit</a>)<br></div><div>and I kn=
ow Casey Carter (and presumably others) were already aware of it before I w=
as.</div><div><br></div><div>I am not aware of any active work specifically=
 on <i>fixing</i> the problem, though. Maybe someone else knows?</div><div>=
<br></div><div>My own observation/hope is that maybe we can develop idioms =
and conventions for concepts, e.g.,</div><div>- We say a concept C is <i>fo=
oish</i> if C&lt;T&gt; implies C&lt;const T&amp;&gt; and vice versa. Every =
concept you write should be <i>fooish</i>.</div><div>- When using the terse=
r syntax, never take a concept-constrained parameter by anything except lva=
lue reference.</div><div><div>- Never take a concept-constrained parameter =
by anything except lvalue reference or by-value (for <i>fooish</i> concepts=
).</div></div><div><br></div><div>If we can get a concise and teachable set=
 of rules for concepts, then IMO it&#39;s okay if you can still hit surpris=
ing bugs (only) by breaking those rules.</div><div><br></div><div>I imagine=
 that the Ranges people must have some kind of informal mental model that t=
hey used in writing Ranges&#39; concepts, even if it hasn&#39;t been formal=
ly written down yet.</div><div><br></div><div>=E2=80=93Arthur</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/96a22750-e9eb-4c08-a5d1-2d0f4d030183%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/96a22750-e9eb-4c08-a5d1-2d0f4d030183=
%40isocpp.org</a>.<br />

------=_Part_41_2071092346.1538007905027--

------=_Part_40_401369106.1538007905026--

.


Author: =?UTF-8?Q?Klaim_=2D_Jo=C3=ABl_Lamotte?= <mjklaim@gmail.com>
Date: Thu, 27 Sep 2018 13:11:34 +0200
Raw View
Hi, I don't really undersand the issue.
Here the requires is applied on T but T can be a reference type,
that's what I understand from the error.
The fix is simple and unrelated to concepts (I mean it's the same
issue without using requires): decay T

#include <type_traits>

template<typename T> requires std::is_integral_v<std::decay_t<T>>
void f(T&&) {}
// http://wg21.link/P1141R0 (YAACD) will make this shorter:
//   void f(std::Integral auto&&) {}

int main()
{
  int i =3D 0;
  f(i); // causes an error because T is int&, not int.
}

This makes the example compile as expected.
On Thu, 27 Sep 2018 at 02:25, Arthur O'Dwyer <arthur.j.odwyer@gmail.com> wr=
ote:
>
> On Wednesday, September 26, 2018 at 10:54:49 AM UTC-7, Kazutoshi SATODA w=
rote:
>>
>> Reading the latest draft N4762 and http://wg21.link/P1141R0 (YAACD),
>> I got worried about possible annoyance in use of concepts with
>> forwarding references.
>>
>> Consider the following example <https://wandbox.org/permlink/mn9eCK2zdCr=
dEFMH>:
>>
>>   #include <type_traits>
>>
>>   template<typename T> requires std::is_integral_v<T>
>>   void f(T&&) {}
>>   // http://wg21.link/P1141R0 (YAACD) will shorten this as:
>>   //   void f(std::Integral auto&&) {}
>>
>>   int main()
>>   {
>>     int i =3D 0;
>>     f(i); // causes an error because T is int&, not int.
>>   }
>>
>> I can fix the verbose version by adding std::remove_reference_t, but I
>> can't figure out how to fix the shortened version without being verbose.
>> If it needs to be verbose and needs remove_reference_t or something, I
>> think it will be somewhat annoying in using or defining concepts.
>>
>> Am I missing something?
>
>
> Nope, this is a real problem.  Here's my contribution to the literature:
> https://quuxplusone.github.io/blog/2018/09/09/constrained-forwarding-refe=
rences/ (reddit)
> and I know Casey Carter (and presumably others) were already aware of it =
before I was.
>
> I am not aware of any active work specifically on fixing the problem, tho=
ugh. Maybe someone else knows?
>
> My own observation/hope is that maybe we can develop idioms and conventio=
ns for concepts, e.g.,
> - We say a concept C is fooish if C<T> implies C<const T&> and vice versa=
.. Every concept you write should be fooish.
> - When using the terser syntax, never take a concept-constrained paramete=
r by anything except lvalue reference.
> - Never take a concept-constrained parameter by anything except lvalue re=
ference or by-value (for fooish concepts).
>
> If we can get a concise and teachable set of rules for concepts, then IMO=
 it's okay if you can still hit surprising bugs (only) by breaking those ru=
les.
>
> I imagine that the Ranges people must have some kind of informal mental m=
odel that they used in writing Ranges' concepts, even if it hasn't been for=
mally written down yet.
>
> =E2=80=93Arthur
>
> --
> 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/isoc=
pp.org/d/msgid/std-proposals/96a22750-e9eb-4c08-a5d1-2d0f4d030183%40isocpp.=
org.

--=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/CAOU91OOCyvz6hSAD89mKOeEUw0N6kyfKkywrqLkFSe15e5g=
QWQ%40mail.gmail.com.

.


Author: =?UTF-8?Q?Klaim_=2D_Jo=C3=ABl_Lamotte?= <mjklaim@gmail.com>
Date: Thu, 27 Sep 2018 13:19:26 +0200
Raw View
To clarify, I mean that this is the equivalent without requires:

template<typename T, typename =3D std::enable_if_t<std::is_integral_v<T>>>
void f(T&&) {}


It's already failing to compile (no matching overload) for the exact
same reason: T is a reference type here
so it's evaluation do not provide the expected result.

Now if you decay T when evaluating it:

template<typename T, typename =3D
std::enable_if_t<std::is_integral_v<std::decay_t<T>>>>
void f(T&&) {}

No error.

On Thu, 27 Sep 2018 at 13:11, Klaim - Jo=C3=ABl Lamotte <mjklaim@gmail.com>=
 wrote:
>
> Hi, I don't really undersand the issue.
> Here the requires is applied on T but T can be a reference type,
> that's what I understand from the error.
> The fix is simple and unrelated to concepts (I mean it's the same
> issue without using requires): decay T
>
> #include <type_traits>
>
> template<typename T> requires std::is_integral_v<std::decay_t<T>>
> void f(T&&) {}
> // http://wg21.link/P1141R0 (YAACD) will make this shorter:
> //   void f(std::Integral auto&&) {}
>
> int main()
> {
>   int i =3D 0;
>   f(i); // causes an error because T is int&, not int.
> }
>
> This makes the example compile as expected.
> On Thu, 27 Sep 2018 at 02:25, Arthur O'Dwyer <arthur.j.odwyer@gmail.com> =
wrote:
> >
> > On Wednesday, September 26, 2018 at 10:54:49 AM UTC-7, Kazutoshi SATODA=
 wrote:
> >>
> >> Reading the latest draft N4762 and http://wg21.link/P1141R0 (YAACD),
> >> I got worried about possible annoyance in use of concepts with
> >> forwarding references.
> >>
> >> Consider the following example <https://wandbox.org/permlink/mn9eCK2zd=
CrdEFMH>:
> >>
> >>   #include <type_traits>
> >>
> >>   template<typename T> requires std::is_integral_v<T>
> >>   void f(T&&) {}
> >>   // http://wg21.link/P1141R0 (YAACD) will shorten this as:
> >>   //   void f(std::Integral auto&&) {}
> >>
> >>   int main()
> >>   {
> >>     int i =3D 0;
> >>     f(i); // causes an error because T is int&, not int.
> >>   }
> >>
> >> I can fix the verbose version by adding std::remove_reference_t, but I
> >> can't figure out how to fix the shortened version without being verbos=
e.
> >> If it needs to be verbose and needs remove_reference_t or something, I
> >> think it will be somewhat annoying in using or defining concepts.
> >>
> >> Am I missing something?
> >
> >
> > Nope, this is a real problem.  Here's my contribution to the literature=
:
> > https://quuxplusone.github.io/blog/2018/09/09/constrained-forwarding-re=
ferences/ (reddit)
> > and I know Casey Carter (and presumably others) were already aware of i=
t before I was.
> >
> > I am not aware of any active work specifically on fixing the problem, t=
hough. Maybe someone else knows?
> >
> > My own observation/hope is that maybe we can develop idioms and convent=
ions for concepts, e.g.,
> > - We say a concept C is fooish if C<T> implies C<const T&> and vice ver=
sa. Every concept you write should be fooish.
> > - When using the terser syntax, never take a concept-constrained parame=
ter by anything except lvalue reference.
> > - Never take a concept-constrained parameter by anything except lvalue =
reference or by-value (for fooish concepts).
> >
> > If we can get a concise and teachable set of rules for concepts, then I=
MO it's okay if you can still hit surprising bugs (only) by breaking those =
rules.
> >
> > I imagine that the Ranges people must have some kind of informal mental=
 model that they used in writing Ranges' concepts, even if it hasn't been f=
ormally written down yet.
> >
> > =E2=80=93Arthur
> >
> > --
> > You received this message because you are subscribed to the Google Grou=
ps "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/is=
ocpp.org/d/msgid/std-proposals/96a22750-e9eb-4c08-a5d1-2d0f4d030183%40isocp=
p.org.

--=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/CAOU91ONV4uPtuqnfsv7Nc9YMrsGKJdoaDV5bmjx-OJs89_u=
kKA%40mail.gmail.com.

.