Topic: decltype_exists() to simplify checks for


Author: Tom Honermann <tom@honermann.net>
Date: Tue, 22 Aug 2017 09:12:34 -0400
Raw View
This is a multi-part message in MIME format.
--------------C8CE185C24484961A2EDC8F8
Content-Type: text/plain; charset="UTF-8"; format=flowed

Are you familiar with std::experimental::is_detected [1] (and friends)
from library fundamentals v2?

The following works and is less verbose than the examples you provided.
It still isn't as terse as what you proposed, but it isn't far off.

#include <experimental/type_traits>

template<typename T>
using AddRef_t = decltype(std::declval<T&>().AddRef());

struct S1 {
};
struct S2 {
   void AddRef();
};

template<typename T>
void AddRef(T& t) {
   if constexpr(std::experimental::is_detected_v<AddRef_t, T>) {
     t.AddRef();
   }
}
void foo(S1 s1, S2 s2) {
   AddRef(s1);
   AddRef(s2);
}

Tom.

[1]: http://en.cppreference.com/w/cpp/experimental/is_detected

On 08/22/2017 06:14 AM, Juliean wrote:
> Right now, there are multiple ways to check if a certain type has a
> member-function/variable, all of which are kind of verbose in their
> own way:
>
> |https://pastebin.com/jCBMnhFa
> |
> Sorry for the link, appearently my code is to long. I've actually been
> preferring approach #2, since #1 required at least 3 function
> overloads to make use of, like in #2.
>
> Yet now with constexpr, it seems more feasable:
>
> |
> |template<typenameObjectType>
> staticvoid AddRef(ObjectType*pObject)
> {
>     if constexpr(hasAddRef<ObjectType>::value)
>         pObject->AddRef();
> }|
> |
>
> However, writing the trait is still a huge amount of unnecessary code.
> While we could just write a macro, I'm trying to stay away from them
> as far as possible, so I'd propose the introduction of a
> language-feature to check for the existance of a certain type/member.
> Its just some though I had recently, so nothing too fleshed out, but
> here's how I'd imagine it could go:
>
> There's a specifier called decltype_exists, which is based on
> decltype. It takes any syntactically valid type-expression, and return
> true whether it actually exists, and false if it doesn't:
>
> |
> ||template<typenameObjectType>
> staticvoid AddRef(ObjectType*pObject)
> {
>     if constexpr(decltype_exists(pObject->AddRef()))
>         pObject->AddRef();
> }||
> |
>
> Thats pretty much it, at least from my requirements so far. Now I
> don't know the implications of implementing such a language construct,
> but I see it to be quite helpfull, now that we are already getting
> tools like if constexpr and fold-expressions that allow us to
> dramatically simplify template-code and make it easier to understand.
>
> What do you think of this? Comments, suggestions, objections? Thanks!
> --
> 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
> <mailto:std-proposals+unsubscribe@isocpp.org>.
> To post to this group, send email to std-proposals@isocpp.org
> <mailto:std-proposals@isocpp.org>.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5f5d3c47-029f-43d8-98e1-34742f41e56d%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5f5d3c47-029f-43d8-98e1-34742f41e56d%40isocpp.org?utm_medium=email&utm_source=footer>.


--
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/dc169025-e53f-a711-fbfc-a2e80e33abd9%40honermann.net.

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

<html>
  <head>
    <meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Type=
">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Are you familiar with
      std::experimental::is_detected [1] (and friends) from library
      fundamentals v2?<br>
      <br>
      The following works and is less verbose than the examples you
      provided.=C2=A0 It still isn't as terse as what you proposed, but it
      isn't far off.<br>
      <br>
      #include &lt;experimental/type_traits&gt;<br>
      <br>
      template&lt;typename T&gt;<br>
      using AddRef_t =3D decltype(std::declval&lt;T&amp;&gt;().AddRef());<b=
r>
      <br>
      struct S1 {<br>
      };<br>
      struct S2 {<br>
      =C2=A0 void AddRef();<br>
      };<br>
      <br>
      template&lt;typename T&gt;<br>
      void AddRef(T&amp; t) {<br>
      =C2=A0 if constexpr(std::experimental::is_detected_v&lt;AddRef_t,
      T&gt;) {<br>
      =C2=A0=C2=A0=C2=A0 t.AddRef();<br>
      =C2=A0 }<br>
      }<br>
      void foo(S1 s1, S2 s2) {<br>
      =C2=A0 AddRef(s1);<br>
      =C2=A0 AddRef(s2);<br>
      }<br>
      <br>
      Tom.<br>
      <br>
      [1]: <a class=3D"moz-txt-link-freetext" href=3D"http://en.cppreferenc=
e.com/w/cpp/experimental/is_detected">http://en.cppreference.com/w/cpp/expe=
rimental/is_detected</a><br>
      <br>
      On 08/22/2017 06:14 AM, Juliean wrote:<br>
    </div>
    <blockquote
      cite=3D"mid:5f5d3c47-029f-43d8-98e1-34742f41e56d@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">Right now, there are multiple ways to check if a
        certain type has a member-function/variable, all of which are
        kind of verbose in their own way:<br>
        <br>
        <code><a class=3D"moz-txt-link-freetext" href=3D"https://pastebin.c=
om/jCBMnhFa">https://pastebin.com/jCBMnhFa</a><br>
        </code><br>
        Sorry for the link, appearently my code is to long. I've
        actually been preferring approach #2, since #1 required at least
        3 function overloads to make use of, like in #2.<br>
        <br>
        Yet now with constexpr, it seems more feasable:<br>
        <br>
        <div style=3D"background-color: rgb(250, 250, 250); border-color:
          rgb(187, 187, 187); border-style: solid; border-width: 1px;
          overflow-wrap: break-word;" class=3D"prettyprint"><code
            class=3D"prettyprint">
            <div class=3D"subprettyprint"><code class=3D"prettyprint"><span
                  style=3D"color: #008;" class=3D"styled-by-prettify">templ=
ate</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&lt;<=
/span><span
                  style=3D"color: #008;" class=3D"styled-by-prettify">typen=
ame</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span
                  style=3D"color: #606;" class=3D"styled-by-prettify">Objec=
tType</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&gt;<=
/span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                </span><span style=3D"color: #008;"
                  class=3D"styled-by-prettify">static</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"> void
                </span><span style=3D"color: #606;"
                  class=3D"styled-by-prettify">AddRef</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span
                  style=3D"color: #606;" class=3D"styled-by-prettify">Objec=
tType</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">*</sp=
an><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">
                  pObject</span><span style=3D"color: #660;"
                  class=3D"styled-by-prettify">)</span><span style=3D"color=
:
                  #000;" class=3D"styled-by-prettify"><br>
                </span><span style=3D"color: #660;"
                  class=3D"styled-by-prettify">{</span><span style=3D"color=
:
                  #000;" class=3D"styled-by-prettify"><br>
                  =C2=A0=C2=A0=C2=A0 if constexpr(hasAddRef&lt;ObjectType&g=
t;::value)<br>
                  =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pObject-&gt;Ad=
dRef();<br>
                  }</span></code><span style=3D"color: #660;"
                class=3D"styled-by-prettify"></span></div>
          </code></div>
        <br>
        However, writing the trait is still a huge amount of unnecessary
        code. While we could just write a macro, I'm trying to stay away
        from them as far as possible, so I'd propose the introduction of
        a language-feature to check for the existance of a certain
        type/member. Its just some though I had recently, so nothing too
        fleshed out, but here's how I'd imagine it could go:<br>
        <br>
        There's a specifier called decltype_exists, which is based on
        decltype. It takes any syntactically valid type-expression, and
        return true whether it actually exists, and false if it doesn't:<br=
>
        <br>
        <div style=3D"background-color: rgb(250, 250, 250); border-color:
          rgb(187, 187, 187); border-style: solid; border-width: 1px;
          overflow-wrap: break-word;" class=3D"prettyprint"><code
            class=3D"prettyprint">
            <div class=3D"subprettyprint"><code class=3D"prettyprint"><code
                  class=3D"prettyprint"><span style=3D"color: #008;"
                    class=3D"styled-by-prettify">template</span><span
                    style=3D"color: #660;" class=3D"styled-by-prettify">&lt=
;</span><span
                    style=3D"color: #008;" class=3D"styled-by-prettify">typ=
ename</span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span
                    style=3D"color: #606;" class=3D"styled-by-prettify">Obj=
ectType</span><span
                    style=3D"color: #660;" class=3D"styled-by-prettify">&gt=
;</span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>
                  </span><span style=3D"color: #008;"
                    class=3D"styled-by-prettify">static</span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify">
                    void </span><span style=3D"color: #606;"
                    class=3D"styled-by-prettify">AddRef</span><span
                    style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span
                    style=3D"color: #606;" class=3D"styled-by-prettify">Obj=
ectType</span><span
                    style=3D"color: #660;" class=3D"styled-by-prettify">*</=
span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify">
                    pObject</span><span style=3D"color: #660;"
                    class=3D"styled-by-prettify">)</span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>
                  </span><span style=3D"color: #660;"
                    class=3D"styled-by-prettify">{</span><span
                    style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>
                    =C2=A0=C2=A0=C2=A0 if
                    constexpr(decltype_exists(pObject-&gt;AddRef()))<br>
                    =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pObject-&gt;=
AddRef();<br>
                    }</span></code></code><span style=3D"color: #660;"
                class=3D"styled-by-prettify"></span></div>
          </code></div>
        <br>
        Thats pretty much it, at least from my requirements so far. Now
        I don't know the implications of implementing such a language
        construct, but I see it to be quite helpfull, now that we are
        already getting tools like if constexpr and fold-expressions
        that allow us to dramatically simplify template-code and make it
        easier to understand. <br>
        <br>
        What do you think of this? Comments, suggestions, objections?
        Thanks!<br>
      </div>
      -- <br>
      You received this message because you are subscribed to the Google
      Groups "ISO C++ Standard - Future Proposals" group.<br>
      To unsubscribe from this group and stop receiving emails from it,
      send an email to <a moz-do-not-send=3D"true"
        href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+=
unsubscribe@isocpp.org</a>.<br>
      To post to this group, send email to <a moz-do-not-send=3D"true"
        href=3D"mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</=
a>.<br>
      To view this discussion on the web visit <a
        moz-do-not-send=3D"true"
href=3D"https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5f5d3c=
47-029f-43d8-98e1-34742f41e56d%40isocpp.org?utm_medium=3Demail&amp;utm_sour=
ce=3Dfooter">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5=
f5d3c47-029f-43d8-98e1-34742f41e56d%40isocpp.org</a>.<br>
    </blockquote>
    <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/dc169025-e53f-a711-fbfc-a2e80e33abd9%=
40honermann.net?utm_medium=3Demail&utm_source=3Dfooter">https://groups.goog=
le.com/a/isocpp.org/d/msgid/std-proposals/dc169025-e53f-a711-fbfc-a2e80e33a=
bd9%40honermann.net</a>.<br />

--------------C8CE185C24484961A2EDC8F8--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Tue, 22 Aug 2017 06:59:39 -0700 (PDT)
Raw View
------=_Part_8360_135208777.1503410379677
Content-Type: multipart/alternative;
 boundary="----=_Part_8361_707977630.1503410379677"

------=_Part_8361_707977630.1503410379677
Content-Type: text/plain; charset="UTF-8"

Writing `hasAddRef` is much easier with concepts now. Indeed, you can just
do this:

if constexpr(requires hasAddRef<ObjectType>)

Though I'd be happy if you could drop the `constexpr` and replace it with
just `requires`:

if requires hasAddRef<ObjectType>


--
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/951b07d5-bf19-4a43-8c95-20a1d94351e8%40isocpp.org.

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

<div dir=3D"ltr">Writing `hasAddRef` is much easier with concepts now. Inde=
ed, you can just do this:<br><br><div style=3D"background-color: rgb(250, 2=
50, 250); border-color: rgb(187, 187, 187); border-style: solid; border-wid=
th: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D"p=
rettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">if</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">constexpr</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">re=
quires hasAddRef</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&lt;</span><span style=3D"color: #606;" class=3D"styled-by-prettify">=
ObjectType</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
&gt;)</span></div></code></div><br>Though I&#39;d be happy if you could dro=
p the `constexpr` and replace it with just `requires`:<br><br><div style=3D=
"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); bo=
rder-style: solid; border-width: 1px; overflow-wrap: break-word;" class=3D"=
prettyprint"><code class=3D"prettyprint"><div class=3D"subprettyprint"><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">if</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> requires hasAddRef</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span st=
yle=3D"color: #606;" class=3D"styled-by-prettify">ObjectType</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span></div></code><=
/div><br><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/951b07d5-bf19-4a43-8c95-20a1d94351e8%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/951b07d5-bf19-4a43-8c95-20a1d94351e8=
%40isocpp.org</a>.<br />

------=_Part_8361_707977630.1503410379677--

------=_Part_8360_135208777.1503410379677--

.


Author: Juliean <galaxy.wars@gmx.at>
Date: Tue, 22 Aug 2017 07:34:24 -0700 (PDT)
Raw View
------=_Part_8472_112975076.1503412464601
Content-Type: multipart/alternative;
 boundary="----=_Part_8473_310109364.1503412464602"

------=_Part_8473_310109364.1503412464602
Content-Type: text/plain; charset="UTF-8"

Am Dienstag, 22. August 2017 15:12:58 UTC+2 schrieb Tom Honermann:
>
> Are you familiar with std::experimental::is_detected [1] (and friends)
> from library fundamentals v2?
>
> The following works and is less verbose than the examples you provided.
> It still isn't as terse as what you proposed, but it isn't far off.
>
>
No, I wasn't familiar with is_detected,  but you are right, it comes closer
to what I was expecting; and just with an std-api, nice. I'd still love to
be able to do this in-place like I suggested, but I'll port to is_detected
as soon as MSVC supports it.


--
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/c21f68fb-f28f-4d13-835d-c71a36f327ff%40isocpp.org.

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

<div dir=3D"ltr">Am Dienstag, 22. August 2017 15:12:58 UTC+2 schrieb Tom Ho=
nermann:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
..8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
 =20
   =20
 =20
  <div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div>Are you familiar with
      std::experimental::is_detected [1] (and friends) from library
      fundamentals v2?<br>
      <br>
      The following works and is less verbose than the examples you
      provided.=C2=A0 It still isn&#39;t as terse as what you proposed, but=
 it
      isn&#39;t far off.<br>
      <br></div></div></blockquote><div><br>No, I wasn&#39;t familiar with =
is_detected,=C2=A0 but you are right, it comes closer to what I was expecti=
ng; and just with an std-api, nice. I&#39;d still love to be able to do thi=
s in-place like I suggested, but I&#39;ll port to is_detected as soon as MS=
VC supports it.<br><br><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/c21f68fb-f28f-4d13-835d-c71a36f327ff%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/c21f68fb-f28f-4d13-835d-c71a36f327ff=
%40isocpp.org</a>.<br />

------=_Part_8473_310109364.1503412464602--

------=_Part_8472_112975076.1503412464601--

.


Author: Juliean <galaxy.wars@gmx.at>
Date: Tue, 22 Aug 2017 07:36:13 -0700 (PDT)
Raw View
------=_Part_3867_1175913943.1503412573087
Content-Type: multipart/alternative;
 boundary="----=_Part_3868_501705551.1503412573087"

------=_Part_3868_501705551.1503412573087
Content-Type: text/plain; charset="UTF-8"

Am Dienstag, 22. August 2017 15:59:39 UTC+2 schrieb Nicol Bolas:
>
> Writing `hasAddRef` is much easier with concepts now. Indeed, you can just
> do this:
>
> if constexpr(requires hasAddRef<ObjectType>)
>
> Though I'd be happy if you could drop the `constexpr` and replace it with
> just `requires`:
>
> if requires hasAddRef<ObjectType>
>
>
I'm looking forward to concepts anyway :) For some reason I didn't think to
use them here - I quess for the same reason as above, in that it still
requires me to actually write the concept (though from what I can see it
would also be just a one-liner.

I now quess that with enough "native" solutions already, there won't be a
high chance something like my proposal would go through.

--
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/7067a21d-f4e2-4106-8bc8-f55727a8566c%40isocpp.org.

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

<div dir=3D"ltr">Am Dienstag, 22. August 2017 15:59:39 UTC+2 schrieb Nicol =
Bolas:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">Writing=
 `hasAddRef` is much easier with concepts now. Indeed, you can just do this=
:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(1=
87,187,187);border-style:solid;border-width:1px"><code><div><span style=3D"=
color:#008">if</span><span style=3D"color:#000"> </span><span style=3D"colo=
r:#008">constexpr</span><span style=3D"color:#660">(</span><span style=3D"c=
olor:#000">requires hasAddRef</span><span style=3D"color:#660">&lt;</span><=
span style=3D"color:#606">ObjectType</span><span style=3D"color:#660">&gt;)=
</span></div></code></div><br>Though I&#39;d be happy if you could drop the=
 `constexpr` and replace it with just `requires`:<br><br><div style=3D"back=
ground-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:so=
lid;border-width:1px"><code><div><span style=3D"color:#008">if</span><span =
style=3D"color:#000"> requires hasAddRef</span><span style=3D"color:#660">&=
lt;</span><span style=3D"color:#606">ObjectType</span><span style=3D"color:=
#660">&gt;</span></div></code></div><br></div></blockquote><div>=C2=A0<br>I=
&#39;m looking forward to concepts anyway :) For some reason I didn&#39;t t=
hink to use them here - I quess for the same reason as above, in that it st=
ill requires me to actually write the concept (though from what I can see i=
t would also be just a one-liner. <br><br>I now quess that with enough &quo=
t;native&quot; solutions already, there won&#39;t be a high chance somethin=
g like my proposal would go through.<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/7067a21d-f4e2-4106-8bc8-f55727a8566c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7067a21d-f4e2-4106-8bc8-f55727a8566c=
%40isocpp.org</a>.<br />

------=_Part_3868_501705551.1503412573087--

------=_Part_3867_1175913943.1503412573087--

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Tue, 22 Aug 2017 17:42:36 +0300
Raw View
On 22 August 2017 at 17:36, Juliean <galaxy.wars@gmx.at> wrote:
> Am Dienstag, 22. August 2017 15:59:39 UTC+2 schrieb Nicol Bolas:
>>
>> Writing `hasAddRef` is much easier with concepts now. Indeed, you can just
>> do this:
>>
>> if constexpr(requires hasAddRef<ObjectType>)
>>
>> Though I'd be happy if you could drop the `constexpr` and replace it with
>> just `requires`:
>>
>> if requires hasAddRef<ObjectType>
>>
>
> I'm looking forward to concepts anyway :) For some reason I didn't think to
> use them here - I quess for the same reason as above, in that it still
> requires me to actually write the concept (though from what I can see it
> would also be just a one-liner.


You can write it as a one-liner, when you're bold enough to enter the
wonderful world of "requires requires":

#include <iostream>

struct X
{
    void addRef() {}
};

struct Y {};

template <class T> void f(T) requires requires(T t) {{t.addRef()}}
{
    std::cout << "template, an addref-supporting type passed" << std::endl;
}

void f(...)
{
    std::cout << "non-template, an addref-unsupporting type passed" <<
std::endl;
}

int main()
{
    f(X{});
    f(Y{});
}


Live demo: https://wandbox.org/permlink/xl2tN1rN2G7LXqwI

--
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/CAFk2RUZNDKf5wZNtKujamZm2wtyGqguf0%2Bs5VF3ELk3LaN-nvg%40mail.gmail.com.

.


Author: Juliean <galaxy.wars@gmx.at>
Date: Tue, 22 Aug 2017 08:17:40 -0700 (PDT)
Raw View
------=_Part_4210_1756722434.1503415060817
Content-Type: multipart/alternative;
 boundary="----=_Part_4211_1092673286.1503415060818"

------=_Part_4211_1092673286.1503415060818
Content-Type: text/plain; charset="UTF-8"



Am Dienstag, 22. August 2017 16:42:39 UTC+2 schrieb Ville Voutilainen:
>
> On 22 August 2017 at 17:36, Juliean <galax...@gmx.at <javascript:>>
> wrote:
> > Am Dienstag, 22. August 2017 15:59:39 UTC+2 schrieb Nicol Bolas:
> >>
> >> Writing `hasAddRef` is much easier with concepts now. Indeed, you can
> just
> >> do this:
> >>
> >> if constexpr(requires hasAddRef<ObjectType>)
> >>
> >> Though I'd be happy if you could drop the `constexpr` and replace it
> with
> >> just `requires`:
> >>
> >> if requires hasAddRef<ObjectType>
> >>
> >
> > I'm looking forward to concepts anyway :) For some reason I didn't think
> to
> > use them here - I quess for the same reason as above, in that it still
> > requires me to actually write the concept (though from what I can see it
> > would also be just a one-liner.
>
>
> You can write it as a one-liner, when you're bold enough to enter the
> wonderful world of "requires requires":
>
> #include <iostream>
>
> struct X
> {
>     void addRef() {}
> };
>
> struct Y {};
>
> template <class T> void f(T) requires requires(T t) {{t.addRef()}}
> {
>     std::cout << "template, an addref-supporting type passed" <<
> std::endl;
> }
>
> void f(...)
> {
>     std::cout << "non-template, an addref-unsupporting type passed" <<
> std::endl;
> }
>
> int main()
> {
>     f(X{});
>     f(Y{});
> }
>
>
> Live demo: https://wandbox.org/permlink/xl2tN1rN2G7LXqwI


This still has the downside of requiring two different functions, in my
case an empty one as a "fallback", as it doesn't use if constexpr.

(at least I've got 3 alternatives for the future at my hands, even if
they're all not perfect; but at least less verbose then the original state).

--
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/1abaffde-287a-4d02-903f-46699d99e09d%40isocpp.org.

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

<div dir=3D"ltr"><br><br>Am Dienstag, 22. August 2017 16:42:39 UTC+2 schrie=
b Ville Voutilainen:<blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 22 Augu=
st 2017 at 17:36, Juliean &lt;<a href=3D"javascript:" target=3D"_blank" gdf=
-obfuscated-mailto=3D"Yppq0ryJCwAJ" rel=3D"nofollow" onmousedown=3D"this.hr=
ef=3D&#39;javascript:&#39;;return true;" onclick=3D"this.href=3D&#39;javasc=
ript:&#39;;return true;">galax...@gmx.at</a>&gt; wrote:
<br>&gt; Am Dienstag, 22. August 2017 15:59:39 UTC+2 schrieb Nicol Bolas:
<br>&gt;&gt;
<br>&gt;&gt; Writing `hasAddRef` is much easier with concepts now. Indeed, =
you can just
<br>&gt;&gt; do this:
<br>&gt;&gt;
<br>&gt;&gt; if constexpr(requires hasAddRef&lt;ObjectType&gt;)
<br>&gt;&gt;
<br>&gt;&gt; Though I&#39;d be happy if you could drop the `constexpr` and =
replace it with
<br>&gt;&gt; just `requires`:
<br>&gt;&gt;
<br>&gt;&gt; if requires hasAddRef&lt;ObjectType&gt;
<br>&gt;&gt;
<br>&gt;
<br>&gt; I&#39;m looking forward to concepts anyway :) For some reason I di=
dn&#39;t think to
<br>&gt; use them here - I quess for the same reason as above, in that it s=
till
<br>&gt; requires me to actually write the concept (though from what I can =
see it
<br>&gt; would also be just a one-liner.
<br>
<br>
<br>You can write it as a one-liner, when you&#39;re bold enough to enter t=
he
<br>wonderful world of &quot;requires requires&quot;:
<br>
<br>#include &lt;iostream&gt;
<br>
<br>struct X
<br>{
<br>=C2=A0 =C2=A0 void addRef() {}
<br>};
<br>
<br>struct Y {};
<br>
<br>template &lt;class T&gt; void f(T) requires requires(T t) {{t.addRef()}=
}
<br>{
<br>=C2=A0 =C2=A0 std::cout &lt;&lt; &quot;template, an addref-supporting t=
ype passed&quot; &lt;&lt; std::endl;
<br>}
<br>
<br>void f(...)
<br>{
<br>=C2=A0 =C2=A0 std::cout &lt;&lt; &quot;non-template, an addref-unsuppor=
ting type passed&quot; &lt;&lt;
<br>std::endl;
<br>}
<br>
<br>int main()
<br>{
<br>=C2=A0 =C2=A0 f(X{});
<br>=C2=A0 =C2=A0 f(Y{});
<br>}
<br>
<br>
<br>Live demo: <a href=3D"https://wandbox.org/permlink/xl2tN1rN2G7LXqwI" ta=
rget=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;https://ww=
w.google.com/url?q\x3dhttps%3A%2F%2Fwandbox.org%2Fpermlink%2Fxl2tN1rN2G7LXq=
wI\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHDhog9Ib6bpx6H1IKkhJD34TetBg&#39=
;;return true;" onclick=3D"this.href=3D&#39;https://www.google.com/url?q\x3=
dhttps%3A%2F%2Fwandbox.org%2Fpermlink%2Fxl2tN1rN2G7LXqwI\x26sa\x3dD\x26sntz=
\x3d1\x26usg\x3dAFQjCNHDhog9Ib6bpx6H1IKkhJD34TetBg&#39;;return true;">https=
://wandbox.org/permlink/<wbr>xl2tN1rN2G7LXqwI</a></blockquote><div><br>This=
 still has the downside of requiring two different functions, in my case an=
 empty one as a &quot;fallback&quot;, as it doesn&#39;t use if constexpr.=
=C2=A0 <br><br>(at least I&#39;ve got 3 alternatives for the future at my h=
ands, even if they&#39;re all not perfect; but at least less verbose then t=
he original state).<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/1abaffde-287a-4d02-903f-46699d99e09d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1abaffde-287a-4d02-903f-46699d99e09d=
%40isocpp.org</a>.<br />

------=_Part_4211_1092673286.1503415060818--

------=_Part_4210_1756722434.1503415060817--

.