Topic: Implicit constexpr


Author: Antony Polukhin <antoshkka@gmail.com>
Date: Tue, 10 Jun 2014 16:22:13 +0400
Raw View
--f46d043891ffb5e4d804fb7a60c6
Content-Type: text/plain; charset=UTF-8

Hi,

(if this is a duplicate then please excuse the noise)

The idea is to allow creation of constexpr variables from expression even
if functions and methods of the expression are not marked with constexpr.

Consider the example:

// Some 3rd party code
struct foo {
    int value_;
    foo(int value = 0) : value_(value) {}
};

int main() {
    // compilation failure
    // non-constexpr constructor 'foo' cannot be used in a constant
expression
    constexpr auto i = foo(100).value_;
}

If we mark constructor of `foo` as constexpr code will compile. The problem
is that there are a lot of unmaintained C++ libraries parts of which could
be used as constexpr, but there is no access to the source code of the
library.

Compiler has all the required info to automatically deduce constexprness of
functions and classes.

Another advantage: a lot of `constexpr` all around the sources make the
code less readable and bloat the size of the sources. Implicit detection of
constexpr will make sources more readable.

--
Best regards,
Antony Polukhin

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

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

<div dir=3D"ltr"><div><div>Hi,<br><br>(if this is a duplicate then please e=
xcuse the noise)<br><br></div>The idea is to allow creation of constexpr va=
riables from expression even if functions and methods of the expression are=
 not marked with constexpr.<br>
<br></div>Consider the example:<br><br>// Some 3rd party code<br>struct foo=
 {<br>=C2=A0=C2=A0=C2=A0 int value_;<br>=C2=A0=C2=A0=C2=A0 foo(int value =
=3D 0) : value_(value) {}<br>};<br><br>int main() { <br>=C2=A0=C2=A0=C2=A0 =
// compilation failure<br>=C2=A0=C2=A0=C2=A0 // non-constexpr constructor &=
#39;foo&#39; cannot be used in a constant expression<br>
=C2=A0=C2=A0=C2=A0 constexpr auto i =3D foo(100).value_;<br>}<br><div><div>=
<br></div><div>If we mark constructor of `foo` as constexpr code will compi=
le. The problem is that there are a lot of unmaintained C++ libraries parts=
 of which could be used as constexpr, but there is no access to the source =
code of the library.<br>
<br></div><div>Compiler has all the required info to automatically deduce c=
onstexprness of functions and classes.<br><br></div><div>Another advantage:=
 a lot of `constexpr` all around the sources make the code less readable an=
d bloat the size of the sources. Implicit detection of constexpr will make =
sources more readable.<br>
</div><div><div><br>-- <br>Best regards,<br>Antony Polukhin
</div></div></div></div>

<p></p>

-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--f46d043891ffb5e4d804fb7a60c6--

.


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Tue, 10 Jun 2014 15:35:56 +0300
Raw View
On 10 June 2014 15:22, Antony Polukhin <antoshkka@gmail.com> wrote:
> Hi,
>
> (if this is a duplicate then please excuse the noise)
>
> The idea is to allow creation of constexpr variables from expression even if
> functions and methods of the expression are not marked with constexpr.


In the original design of constexpr this idea met so much resistance that it
was infeasible. Even nowadays, the standard library implementations are
not allowed to mark library functions constexpr due to possible portability
problems, so it's far-fetched that we would be able to establish a clear
set of portable rules according to which compilers agree on what is or
is not a constexpr if the syntactic marker is not present. That doesn't prevent
one from trying, but it's an uphill battle.

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: David Krauss <potswa@gmail.com>
Date: Tue, 10 Jun 2014 21:33:37 +0800
Raw View
--Apple-Mail=_D014A97B-C7D4-469D-8057-981EB2B4F16C
Content-Type: text/plain; charset=ISO-8859-1


On 2014-06-10, at 8:22 PM, Antony Polukhin <antoshkka@gmail.com> wrote:

> Hi,
>
> (if this is a duplicate then please excuse the noise)

Yep, but it's a pretty subtle issue. See this StackOverflow Q&A, which links back to this discussion group:

 Why do we need to mark functions as constexpr?

The idea is similar for variables: attempting to deduce constant-ness is still dangerous, even when it's feasible.

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--Apple-Mail=_D014A97B-C7D4-469D-8057-981EB2B4F16C
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;06&ndash;10, at 8:22 PM, Antony Polukhin &lt;<a href=3D"mailto:antosh=
kka@gmail.com">antoshkka@gmail.com</a>&gt; wrote:</div><br class=3D"Apple-i=
nterchange-newline"><blockquote type=3D"cite"><div dir=3D"ltr"><div><div>Hi=
,<br><br>(if this is a duplicate then please excuse the noise)<br></div></d=
iv></div></blockquote><div><br></div><div>Yep, but it&rsquo;s a pretty subt=
le issue. See this StackOverflow Q&amp;A, which links back to this discussi=
on group:</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre"> </span><a href=3D"http://stackoverflow.com/a/19830673/153=
285">Why do we need to mark functions as constexpr?</a></div><div><br></div=
><div>The idea is similar for variables: attempting to deduce constant-ness=
 is still dangerous, even when it&rsquo;s feasible.</div></div><div><br></d=
iv></body></html>

<p></p>

-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--Apple-Mail=_D014A97B-C7D4-469D-8057-981EB2B4F16C--

.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Tue, 10 Jun 2014 11:32:54 -0700
Raw View
--001a1133b4a4614af904fb7f8eea
Content-Type: text/plain; charset=UTF-8

On Tue, Jun 10, 2014 at 5:22 AM, Antony Polukhin <antoshkka@gmail.com>
wrote:

> Hi,
>
> (if this is a duplicate then please excuse the noise)
>
> The idea is to allow creation of constexpr variables from expression even
> if functions and methods of the expression are not marked with constexpr.
>
> Consider the example:
>
> // Some 3rd party code
> struct foo {
>     int value_;
>     foo(int value = 0) : value_(value) {}
> };
>
> int main() {
>     // compilation failure
>     // non-constexpr constructor 'foo' cannot be used in a constant
> expression
>     constexpr auto i = foo(100).value_;
> }
>
> If we mark constructor of `foo` as constexpr code will compile. The
> problem is that there are a lot of unmaintained C++ libraries parts of
> which could be used as constexpr, but there is no access to the source code
> of the library.
>

Today, that third-party code can move its constructor definition out of
line, and the only impact that has on its consumers is a possible
performance change. With your proposed change, the third-party library
would *break source compatibility* every time it moved an inline function
out of line, or otherwise changed the set of cases where that function
could be part of a constant expression (for instance, if they added logging
to the function).

That's a horrible burden to impose on libraries. The status quo is that
library authors have to opt into this compatibility burden by marking their
functions as 'constexpr', and I think that's appropriate.

Compiler has all the required info to automatically deduce constexprness of
> functions and classes.
>
> Another advantage: a lot of `constexpr` all around the sources make the
> code less readable and bloat the size of the sources. Implicit detection of
> constexpr will make sources more readable.
>
> --
> Best regards,
> Antony Polukhin
>
> --
>
> ---
> 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.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

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

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On T=
ue, Jun 10, 2014 at 5:22 AM, Antony Polukhin <span dir=3D"ltr">&lt;<a href=
=3D"mailto:antoshkka@gmail.com" target=3D"_blank">antoshkka@gmail.com</a>&g=
t;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><div>Hi,<br><br>(if th=
is is a duplicate then please excuse the noise)<br><br></div>The idea is to=
 allow creation of constexpr variables from expression even if functions an=
d methods of the expression are not marked with constexpr.<br>

<br></div>Consider the example:<br><br>// Some 3rd party code<br>struct foo=
 {<br>=C2=A0=C2=A0=C2=A0 int value_;<br>=C2=A0=C2=A0=C2=A0 foo(int value =
=3D 0) : value_(value) {}<br>};<br><br>int main() { <br>=C2=A0=C2=A0=C2=A0 =
// compilation failure<br>=C2=A0=C2=A0=C2=A0 // non-constexpr constructor &=
#39;foo&#39; cannot be used in a constant expression<br>

=C2=A0=C2=A0=C2=A0 constexpr auto i =3D foo(100).value_;<br>}<br><div><div>=
<br></div><div>If we mark constructor of `foo` as constexpr code will compi=
le. The problem is that there are a lot of unmaintained C++ libraries parts=
 of which could be used as constexpr, but there is no access to the source =
code of the library.<br>
</div></div></div></blockquote><div><br></div><div>Today, that third-party =
code can move its constructor definition out of line, and the only impact t=
hat has on its consumers is a possible performance change. With your propos=
ed change, the third-party library would *break source compatibility* every=
 time it moved an inline function out of line, or otherwise changed the set=
 of cases where that function could be part of a constant expression (for i=
nstance, if they added logging to the function).</div>
<div><br></div><div>That&#39;s a horrible burden to impose on libraries. Th=
e status quo is that library authors have to opt into this compatibility bu=
rden by marking their functions as &#39;constexpr&#39;, and I think that&#3=
9;s appropriate.</div>
<div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><div></=
div><div>Compiler has all the required info to automatically deduce constex=
prness of functions and classes.<br>
<br></div><div>Another advantage: a lot of `constexpr` all around the sourc=
es make the code less readable and bloat the size of the sources. Implicit =
detection of constexpr will make sources more readable.<span class=3D"HOEnZ=
b"><font color=3D"#888888"><br>

</font></span></div><span class=3D"HOEnZb"><font color=3D"#888888"><div><di=
v><br>-- <br>Best regards,<br>Antony Polukhin
</div></div></font></span></div></div><span class=3D"HOEnZb"><font color=3D=
"#888888">

<p></p>

-- <br>
<br>
--- <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@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>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br></div></div>

<p></p>

-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a1133b4a4614af904fb7f8eea--

.