Topic: Proposal "is_constexpr


Author: dimeiyi24@gmail.com
Date: Mon, 17 Jul 2017 03:15:53 -0700 (PDT)
Raw View
------=_Part_1105_515843791.1500286553806
Content-Type: multipart/alternative;
 boundary="----=_Part_1106_530145505.1500286553806"

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

=E2=96=A1 Motivation
In C++, the same semantic functions sometimes have to write two different=
=20
versions, a constexpr version, an efficient general version.
For example:
constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++;return s-tp=
;}
auto StrLenSSE4(const char *s){...}
=E2=96=A1 Proposal
Gcc has a built-in function __builtin_constant_p (v) to determine whether a=
=20
value is a compile time constant, but it can only be used for macros, and v=
=20
can only be one literal.
Can you provide a function of *is_constexpr*(v) to determine whether v is a=
=20
compile time constant?
In this way, the function above can use a unified interface.
constexpr auto StrLen(const char *s){
  if constexpr(*is_constexpr*(s)) {
    auto tp=3Ds;while(*s) s++;return s-tp;
  }
  else return StrLenSSE4(s);
}

--=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/803a7b14-34c2-4ee6-8c5a-154532143a6e%40isocpp.or=
g.

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

<div dir=3D"ltr"><font color=3D"#000000"><font color=3D"#0000ff">=E2=96=A1 =
Motivation</font><br>In C++, the same semantic functions sometimes have to =
write two different versions, a constexpr version, an efficient general ver=
sion.<br>For example:<br>constexpr auto StrLen(const char *s) {auto tp=3Ds;=
while(*s) s++;return s-tp;}<br>auto StrLenSSE4(const char *s){...}<br><font=
 color=3D"#0000ff">=E2=96=A1 Proposal</font><br>Gcc has a built-in function=
 __builtin_constant_p (v) to determine whether a value is a compile time co=
nstant, but it can only be used for macros, and v can only be one literal.<=
br>Can you provide a function of <font color=3D"#000000"><strong>is_constex=
pr</strong></font>(v) to determine whether v is a compile time constant?<br=
>In this way, the function above can use a unified interface.<br>constexpr =
auto StrLen(const char *s){<br>=C2=A0 if constexpr(<strong>is_constexpr</st=
rong>(s)) {<br>=C2=A0=C2=A0=C2=A0 auto tp=3Ds;while(*s) s++;return s-tp;<br=
>=C2=A0 }<br>=C2=A0 else return StrLenSSE4(s);<br>}</font></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/803a7b14-34c2-4ee6-8c5a-154532143a6e%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/803a7b14-34c2-4ee6-8c5a-154532143a6e=
%40isocpp.org</a>.<br />

------=_Part_1106_530145505.1500286553806--

------=_Part_1105_515843791.1500286553806--

.


Author: Daemon Snake <swac31@gmail.com>
Date: Mon, 17 Jul 2017 17:13:42 +0200
Raw View
--001a113f73d863d2cb055484d982
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

is_constexp(s) is not possible, nor likely currently as function arguments
are never constexpr.
The subject is highly controversial (possible ODR violations, etc...).
Read this discussion:
https://groups.google.com/a/isocpp.org/forum/?fromgroups#!search/is_constex=
pr/std-proposals/NErPRYCz3ZU/AOcjouU8BAAJ




2017-07-17 12:15 GMT+02:00 <dimeiyi24@gmail.com>:

> =E2=96=A1 Motivation
> In C++, the same semantic functions sometimes have to write two different
> versions, a constexpr version, an efficient general version.
> For example:
> constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++;return s-=
tp;}
> auto StrLenSSE4(const char *s){...}
> =E2=96=A1 Proposal
> Gcc has a built-in function __builtin_constant_p (v) to determine whether
> a value is a compile time constant, but it can only be used for macros, a=
nd
> v can only be one literal.
> Can you provide a function of *is_constexpr*(v) to determine whether v is
> a compile time constant?
> In this way, the function above can use a unified interface.
> constexpr auto StrLen(const char *s){
>   if constexpr(*is_constexpr*(s)) {
>     auto tp=3Ds;while(*s) s++;return s-tp;
>   }
>   else return StrLenSSE4(s);
> }
>
> --
> 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/803a7b14-34c2-4ee6-
> 8c5a-154532143a6e%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/803a7b14-34=
c2-4ee6-8c5a-154532143a6e%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoot=
er>
> .
>

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

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

<div dir=3D"ltr"><div>Hi,</div><div><br></div>is_constexp(s) is not possibl=
e, nor likely currently as function arguments are never constexpr.<div>The =
subject is highly controversial (possible ODR violations, etc...).</div><di=
v>Read this discussion: <a href=3D"https://groups.google.com/a/isocpp.org/f=
orum/?fromgroups#!search/is_constexpr/std-proposals/NErPRYCz3ZU/AOcjouU8BAA=
J">https://groups.google.com/a/isocpp.org/forum/?fromgroups#!search/is_cons=
texpr/std-proposals/NErPRYCz3ZU/AOcjouU8BAAJ</a></div><div><div><br></div><=
div><br></div><div><div><br></div></div></div></div><div class=3D"gmail_ext=
ra"><br><div class=3D"gmail_quote">2017-07-17 12:15 GMT+02:00  <span dir=3D=
"ltr">&lt;<a href=3D"mailto:dimeiyi24@gmail.com" target=3D"_blank">dimeiyi2=
4@gmail.com</a>&gt;</span>:<br><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"l=
tr"><font color=3D"#000000"><font color=3D"#0000ff">=E2=96=A1 Motivation</f=
ont><br>In C++, the same semantic functions sometimes have to write two dif=
ferent versions, a constexpr version, an efficient general version.<br>For =
example:<br>constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++=
;return s-tp;}<br>auto StrLenSSE4(const char *s){...}<br><font color=3D"#00=
00ff">=E2=96=A1 Proposal</font><br>Gcc has a built-in function __builtin_co=
nstant_p (v) to determine whether a value is a compile time constant, but i=
t can only be used for macros, and v can only be one literal.<br>Can you pr=
ovide a function of <font color=3D"#000000"><strong>is_constexpr</strong></=
font>(v) to determine whether v is a compile time constant?<br>In this way,=
 the function above can use a unified interface.<br>constexpr auto StrLen(c=
onst char *s){<br>=C2=A0 if constexpr(<strong>is_constexpr</strong>(s)) {<b=
r>=C2=A0=C2=A0=C2=A0 auto tp=3Ds;while(*s) s++;return s-tp;<br>=C2=A0 }<br>=
=C2=A0 else return StrLenSSE4(s);<br>}</font></div><span class=3D"HOEnZb"><=
font color=3D"#888888">

<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" target=3D"_=
blank">std-proposals+unsubscribe@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">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/803a7b14-34c2-4ee6-8c5a-154532143a6e%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/803a=
7b14-34c2-4ee6-<wbr>8c5a-154532143a6e%40isocpp.org</a><wbr>.<br>
</font></span></blockquote></div><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/CAAE_HdJWNqFYUJvCDGGXE4oYF6vhVd5iFY3a=
tKPjtH9pNRrAqg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAAE_HdJWNqFYUJvC=
DGGXE4oYF6vhVd5iFY3atKPjtH9pNRrAqg%40mail.gmail.com</a>.<br />

--001a113f73d863d2cb055484d982--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 17 Jul 2017 08:14:50 -0700 (PDT)
Raw View
------=_Part_1332_1262332928.1500304490454
Content-Type: multipart/alternative;
 boundary="----=_Part_1333_1375528074.1500304490455"

------=_Part_1333_1375528074.1500304490455
Content-Type: text/plain; charset="UTF-8"

I know there are a lot of papers out there
<http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2017/>. But does it
really take that long to search them for "constexpr" to see if this has
already been proposed
<http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0595r0.html>? You
can even do a Google site-specific search to just search WG21's papers.

And, as shown in that paper, checking for the `constexpr` status of a
variable is rather pointless. What you really want to ask is "is this code
being run at compile-time?"

--
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/be511dfd-c3f1-4e02-9304-48dabe8300c9%40isocpp.org.

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

<div dir=3D"ltr">I know there are <a href=3D"http://www.open-std.org/JTC1/S=
C22/WG21/docs/papers/2017/">a lot of papers out there</a>. But does it real=
ly take that long to search them for &quot;constexpr&quot; to see if this <=
a href=3D"http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0595r0.h=
tml">has already been proposed</a>? You can even do a Google site-specific =
search to just search WG21&#39;s papers.<br><br>And, as shown in that paper=
, checking for the `constexpr` status of a variable is rather pointless. Wh=
at you really want to ask is &quot;is this code being run at compile-time?&=
quot;<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/be511dfd-c3f1-4e02-9304-48dabe8300c9%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/be511dfd-c3f1-4e02-9304-48dabe8300c9=
%40isocpp.org</a>.<br />

------=_Part_1333_1375528074.1500304490455--

------=_Part_1332_1262332928.1500304490454--

.


Author: "T. C." <rs2740@gmail.com>
Date: Mon, 17 Jul 2017 13:48:53 -0700 (PDT)
Raw View
------=_Part_321_1063119042.1500324533685
Content-Type: multipart/alternative;
 boundary="----=_Part_322_1995262505.1500324533685"

------=_Part_322_1995262505.1500324533685
Content-Type: text/plain; charset="UTF-8"


>
> Gcc has a built-in function __builtin_constant_p (v) to determine whether
> a value is a compile time constant, but it can only be used for macros, and
> v can only be one literal.
>

That's so not how __builtin_constant_p works.

--
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/6e485787-ee7c-4271-88cc-90f42cf22e4f%40isocpp.org.

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

<div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"margin: 0px 0px=
 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">=
Gcc has a built-in function __builtin_constant_p (v) to determine whether a=
 value is a compile time constant, but it can only be used for macros, and =
v can only be one literal.<br></blockquote><br>That&#39;s so not how __buil=
tin_constant_p works.<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/6e485787-ee7c-4271-88cc-90f42cf22e4f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/6e485787-ee7c-4271-88cc-90f42cf22e4f=
%40isocpp.org</a>.<br />

------=_Part_322_1995262505.1500324533685--

------=_Part_321_1063119042.1500324533685--

.


Author: Erich Keane <erich.keane@verizon.net>
Date: Mon, 17 Jul 2017 15:27:49 -0700 (PDT)
Raw View
------=_Part_1454_174121304.1500330469796
Content-Type: multipart/alternative;
 boundary="----=_Part_1455_1090816499.1500330469796"

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

In Kona, Daveed proposed this:=20
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0595r0.html

it made it through EWG, and seems to accomplish your goal.  Additionally,=
=20
since "is_constexpr" is a compile-time constant, the constant folder level=
=20
of optimization will just 'fix' it for the 'false' case.

BTW: No progress was made by Daveed in Toronto, but no idea if that was on=
=20
purpose, or just a ball dropped.

On Monday, July 17, 2017 at 3:15:53 AM UTC-7, dime...@gmail.com wrote:
>
> =E2=96=A1 Motivation
> In C++, the same semantic functions sometimes have to write two different=
=20
> versions, a constexpr version, an efficient general version.
> For example:
> constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++;return s-=
tp;}
> auto StrLenSSE4(const char *s){...}
> =E2=96=A1 Proposal
> Gcc has a built-in function __builtin_constant_p (v) to determine whether=
=20
> a value is a compile time constant, but it can only be used for macros, a=
nd=20
> v can only be one literal.
> Can you provide a function of *is_constexpr*(v) to determine whether v is=
=20
> a compile time constant?
> In this way, the function above can use a unified interface.
> constexpr auto StrLen(const char *s){
>   if constexpr(*is_constexpr*(s)) {
>     auto tp=3Ds;while(*s) s++;return s-tp;
>   }
>   else return StrLenSSE4(s);
> }
>

--=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/93d6544c-8e99-4f68-ab84-b355a0ce5ca9%40isocpp.or=
g.

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

<div dir=3D"ltr">In Kona, Daveed proposed this: http://www.open-std.org/jtc=
1/sc22/wg21/docs/papers/2017/p0595r0.html<br><br>it made it through EWG, an=
d seems to accomplish your goal.=C2=A0 Additionally, since &quot;is_constex=
pr&quot; is a compile-time constant, the constant folder level of optimizat=
ion will just &#39;fix&#39; it for the &#39;false&#39; case.<br><br>BTW: No=
 progress was made by Daveed in Toronto, but no idea if that was on purpose=
, or just a ball dropped.<br><br>On Monday, July 17, 2017 at 3:15:53 AM UTC=
-7, dime...@gmail.com wrote:<blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><d=
iv dir=3D"ltr"><font color=3D"#000000"><font color=3D"#0000ff">=E2=96=A1 Mo=
tivation</font><br>In C++, the same semantic functions sometimes have to wr=
ite two different versions, a constexpr version, an efficient general versi=
on.<br>For example:<br>constexpr auto StrLen(const char *s) {auto tp=3Ds;wh=
ile(*s) s++;return s-tp;}<br>auto StrLenSSE4(const char *s){...}<br><font c=
olor=3D"#0000ff">=E2=96=A1 Proposal</font><br>Gcc has a built-in function _=
_builtin_constant_p (v) to determine whether a value is a compile time cons=
tant, but it can only be used for macros, and v can only be one literal.<br=
>Can you provide a function of <font color=3D"#000000"><b>is_constexpr</b><=
/font>(v) to determine whether v is a compile time constant?<br>In this way=
, the function above can use a unified interface.<br>constexpr auto StrLen(=
const char *s){<br>=C2=A0 if constexpr(<b>is_constexpr</b>(s)) {<br>=C2=A0=
=C2=A0=C2=A0 auto tp=3Ds;while(*s) s++;return s-tp;<br>=C2=A0 }<br>=C2=A0 e=
lse return StrLenSSE4(s);<br>}</font></div></blockquote></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/93d6544c-8e99-4f68-ab84-b355a0ce5ca9%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/93d6544c-8e99-4f68-ab84-b355a0ce5ca9=
%40isocpp.org</a>.<br />

------=_Part_1455_1090816499.1500330469796--

------=_Part_1454_174121304.1500330469796--

.


Author: Pedro Alves <pedro@palves.net>
Date: Tue, 18 Jul 2017 14:20:34 +0100
Raw View
On 07/17/2017 11:15 AM, dimeiyi24@gmail.com wrote:
> =E2=96=A1 Motivation
> In C++, the same semantic functions sometimes have to write two
> different versions, a constexpr version, an efficient general version.
> For example:
> constexpr auto StrLen(const char *s) {auto tp=3Ds;while(*s) s++;return s-=
tp;}
> auto StrLenSSE4(const char *s){...}
> =E2=96=A1 Proposal
> Gcc has a built-in function __builtin_constant_p (v) to determine
> whether a value is a compile time constant, but it can only be used for
> macros, and v can only be one literal.
> Can you provide a function of *is_constexpr*(v) to determine whether v
> is a compile time constant?
> In this way, the function above can use a unified interface.
> constexpr auto StrLen(const char *s){
>   if constexpr(*is_constexpr*(s)) {
>     auto tp=3Ds;while(*s) s++;return s-tp;
>   }
>   else return StrLenSSE4(s);
> }

Note that GCC's __builtin_constant_p _can_ be used for the
above.  It's how C++17 constexpr char_traits<>::length() etc.
is currently implemented in libstdc++ trunk.  (BTW, you can
just use char_traits<>::length() in this case.)

#include <string.h>

static constexpr bool
constant_string_p(const char* s)
{
  while (__builtin_constant_p(*s) && *s)
    s++;
  return __builtin_constant_p(*s);
}

static constexpr size_t constexpr_strlen(const char* s)
{
  auto tp =3D s;
  while (*s)
    s++;
  return s - tp;
}

constexpr size_t my_strlen(const char* s) noexcept
{
  if (constant_string_p(s))
    return constexpr_strlen(s);
  return strlen(s);
}

static_assert (my_strlen ("") =3D=3D 0);
static_assert (my_strlen ("hello") =3D=3D 5);
static_assert (my_strlen ("he\0llo") =3D=3D 2);

static const char array[] =3D "foo";
static_assert (my_strlen (array) =3D=3D 3);

constexpr bool check()
{
  char s[] =3D "str";
  s[0] =3D 'l';
  s[1] =3D '\0';
  return my_strlen(s) =3D=3D 1;
}

static_assert(check());

int main (int argc, char** argv)
{
  return my_strlen(argv[0]);
}

$ g++ constexpr_strlen.cc -o constexpr_strlen -g3 -O2 -std=3Dgnu++17=20
$ gdb --batch -q ./constexpr_strlen -ex "disassemble main"
Dump of assembler code for function main(int, char**):
   0x00000000004004c0 <+0>:     sub    $0x8,%rsp
   0x00000000004004c4 <+4>:     mov    (%rsi),%rdi
   0x00000000004004c7 <+7>:     callq  0x400490 <strlen@plt>
   0x00000000004004cc <+12>:    add    $0x8,%rsp
   0x00000000004004d0 <+16>:    retq  =20
End of assembler dump.
$

--=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/c395066b-4639-3abb-b923-63040818904c%40palves.ne=
t.

.