Topic: decltype_exists() to simplify checks for member/type existance
Author: Juliean <galaxy.wars@gmx.at>
Date: Tue, 22 Aug 2017 03:14:09 -0700 (PDT)
Raw View
------=_Part_8067_1990968694.1503396849806
Content-Type: multipart/alternative;
boundary="----=_Part_8068_170741820.1503396849806"
------=_Part_8068_170741820.1503396849806
Content-Type: text/plain; charset="UTF-8"
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<typename ObjectType>
static void 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<typename ObjectType>
static void 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.
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/5f5d3c47-029f-43d8-98e1-34742f41e56d%40isocpp.org.
------=_Part_8068_170741820.1503396849806
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Right now, there are multiple ways to check if a certain t=
ype has a=20
member-function/variable, all of which are kind of verbose in their own=20
way:<br><br><code>https://pastebin.com/jCBMnhFa<br></code><br>Sorry for the=
link, appearently my code is to long. I've actually been preferring ap=
proach #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, 1=
87, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word=
;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subprett=
yprint"><code class=3D"prettyprint"><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">template</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify"><</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">typename</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">=
ObjectType</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: #008;" class=3D"styled-by-prettify">static</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> void </span><sp=
an style=3D"color: #606;" class=3D"styled-by-prettify">AddRef</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">ObjectType</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">*</span><span style=3D"color: #00=
0;" 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"st=
yled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0=C2=A0=C2=A0 if constexpr(hasAddRef<ObjectType>::v=
alue)<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pObject->AddRef();<b=
r>}</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=20
could just write a macro, I'm trying to stay away from them as far as=
=20
possible, so I'd propose the introduction of a language-feature to chec=
k
for the existance of a certain type/member. Its just some though I had=20
recently, so nothing too fleshed out, but here's how I'd imagine it=
=20
could go:<br><br>There's a specifier called decltype_exists, which is=
=20
based on decltype. It takes any syntactically valid type-expression, and
return true whether it actually exists, and false if it doesn't:<br><b=
r><div style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187=
, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: break-w=
ord;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subpr=
ettyprint"><code class=3D"prettyprint"><code class=3D"prettyprint"><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">template</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">typename</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;=
" class=3D"styled-by-prettify">ObjectType</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">></span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">static</span><span style=3D"color: #000;" class=3D"style=
d-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-pre=
ttify">(</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Ob=
jectType</span><span style=3D"color: #660;" class=3D"styled-by-prettify">*<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> pObject</s=
pan><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(d=
ecltype_exists(pObject->AddRef()))<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 pObject->AddRef();<br>}</span></code></code><span style=3D"col=
or: #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=
=20
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=20
constexpr and fold-expressions that allow us to dramatically simplify=20
template-code and make it easier to understand. <br><br>What do you think o=
f this? Comments, suggestions, objections? Thanks!<br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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/5f5d3c47-029f-43d8-98e1-34742f41e56d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5f5d3c47-029f-43d8-98e1-34742f41e56d=
%40isocpp.org</a>.<br />
------=_Part_8068_170741820.1503396849806--
------=_Part_8067_1990968694.1503396849806--
.