Topic: constexpr functions without constexpr


Author: Thiago Macieira <thiago@macieira.org>
Date: Mon, 20 Oct 2014 20:00:02 +0800
Raw View
On Monday 20 October 2014 03:26:32 Germ=E1n Diago wrote:
> > For example, it would be nice in Lua or Squirrel, to be able to use the=
se
> > functions as constexpr to embed the bytecode directly.
> > Otherwise, if you want to achieve this effect, you have to basically
> > reimplement everything only because the original API
> > won't work at compile-time.
>=20
> Reading again  makes me see how inaccurate I was. Imagine you have:
>=20
> char * compile_string(const char * str);
>=20
> It would be nice to be able to use that in a metaprogram and generate a
> char array somewhere. I don't know if it is understandable.
> This way, this API could be used at compile-time. So during development,
> you use the scripts at runtime. Designers can, many people
> can. When delivering, everything is inside the executable, including the
> bytecode or the machine code to be executed, without any
> runtime overhead. Not the strings to be compiled. This would be specially
> useful in not so powerful devices.

When compiling for release mode, can't you simply add the extra constexpr t=
o=20
the functions that need to be constexpr?

#ifdef NDEBUG
constexpr Program compile_program(const char *str)
{
 /* constexpr implementation goes here */
}
#else
Program compile_program(const char *str)=20
{ return compile_program_noninline(str); }
#endif

--=20
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

--=20

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

.


Author: =?UTF-8?Q?Germ=C3=A1n_Diago?= <germandiago@gmail.com>
Date: Mon, 20 Oct 2014 07:26:19 -0700 (PDT)
Raw View
------=_Part_87_6101526.1413815180655
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



El lunes, 20 de octubre de 2014 19:00:38 UTC+7, Thiago Macieira escribi=C3=
=B3:
>
> On Monday 20 October 2014 03:26:32 Germ=C3=A1n Diago wrote:=20
> > > For example, it would be nice in Lua or Squirrel, to be able to use=
=20
> these=20
> > > functions as constexpr to embed the bytecode directly.=20
> > > Otherwise, if you want to achieve this effect, you have to basically=
=20
> > > reimplement everything only because the original API=20
> > > won't work at compile-time.=20
> >=20
> > Reading again  makes me see how inaccurate I was. Imagine you have:=20
> >=20
> > char * compile_string(const char * str);=20
> >=20
> > It would be nice to be able to use that in a metaprogram and generate a=
=20
> > char array somewhere. I don't know if it is understandable.=20
> > This way, this API could be used at compile-time. So during development=
,=20
> > you use the scripts at runtime. Designers can, many people=20
> > can. When delivering, everything is inside the executable, including th=
e=20
> > bytecode or the machine code to be executed, without any=20
> > runtime overhead. Not the strings to be compiled. This would be=20
> specially=20
> > useful in not so powerful devices.=20
>
> When compiling for release mode, can't you simply add the extra constexpr=
=20
> to=20
> the functions that need to be constexpr?=20
>
> #ifdef NDEBUG=20
> constexpr Program compile_program(const char *str)=20
> {=20
>         /* constexpr implementation goes here */=20
> }=20
> #else=20
> Program compile_program(const char *str)=20
> { return compile_program_noninline(str); }=20
> #endif=20
>
> Well, maybe in this case I can modify the code, you are right about that,=
=20
didn't think of this. Thanks.=20
About a general way: would this be possible?=20
I think the function would have to be exposed in the header file if I am=20
right.

=20

--=20

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

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

<div dir=3D"ltr"><br><br>El lunes, 20 de octubre de 2014 19:00:38 UTC+7, Th=
iago Macieira  escribi=C3=B3:<blockquote class=3D"gmail_quote" style=3D"mar=
gin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">O=
n Monday 20 October 2014 03:26:32 Germ=C3=A1n Diago wrote:
<br>&gt; &gt; For example, it would be nice in Lua or Squirrel, to be able =
to use these
<br>&gt; &gt; functions as constexpr to embed the bytecode directly.
<br>&gt; &gt; Otherwise, if you want to achieve this effect, you have to ba=
sically
<br>&gt; &gt; reimplement everything only because the original API
<br>&gt; &gt; won't work at compile-time.
<br>&gt;=20
<br>&gt; Reading again &nbsp;makes me see how inaccurate I was. Imagine you=
 have:
<br>&gt;=20
<br>&gt; char * compile_string(const char * str);
<br>&gt;=20
<br>&gt; It would be nice to be able to use that in a metaprogram and gener=
ate a
<br>&gt; char array somewhere. I don't know if it is understandable.
<br>&gt; This way, this API could be used at compile-time. So during develo=
pment,
<br>&gt; you use the scripts at runtime. Designers can, many people
<br>&gt; can. When delivering, everything is inside the executable, includi=
ng the
<br>&gt; bytecode or the machine code to be executed, without any
<br>&gt; runtime overhead. Not the strings to be compiled. This would be sp=
ecially
<br>&gt; useful in not so powerful devices.
<br>
<br>When compiling for release mode, can't you simply add the extra constex=
pr to=20
<br>the functions that need to be constexpr?
<br>
<br>#ifdef NDEBUG
<br>constexpr Program compile_program(const char *str)
<br>{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* constexpr implementa=
tion goes here */
<br>}
<br>#else
<br>Program compile_program(const char *str)=20
<br>{ return compile_program_noninline(str)<wbr>; }
<br>#endif
<br><br></blockquote><div>Well, maybe in this case I can modify the code, y=
ou are right about that, didn't think of this. Thanks.&nbsp;</div><div>Abou=
t a general way: would this be possible?&nbsp;</div><div>I think the functi=
on would have to be exposed in the header file if I am right.</div><div><br=
></div><div>&nbsp;</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 />

------=_Part_87_6101526.1413815180655--

.


Author: Thiago Macieira <thiago@macieira.org>
Date: Mon, 20 Oct 2014 22:51:05 +0800
Raw View
On Monday 20 October 2014 07:26:19 Germ=E1n Diago wrote:
> About a general way: would this be possible?=20

I don't think that's the objective of constexpr. The idea is that the compi=
ler=20
is able to calculate the result at compile-time if the input is constexpr t=
oo,=20
so I don't see the point in not doing that if it could.

Though note that constexpr functions are allowed to call non-constexpr code=
,=20
provided that it never reaches that condition if the input is constexpr and=
=20
the variable declared constexpr too. That means you can switch to runtime=
=20
calculation by passing a parameter of a different value. Such as:

enum CalculationTime { Now, Later };
constexpr Program compile_program(const char *str, CalculationTime time)
{
 if (time =3D=3D Later)
  return compile_program_noninline(str);

        /* constexpr implementation goes here */
}

That could also be a global variable.

> I think the function would have to be exposed in the header file if I am=
=20
> right.

Correct. But that's besides the point: you started with the assumption that=
=20
you have the constexpr function exists.

--=20
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

--=20

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

.


Author: =?UTF-8?Q?Germ=C3=A1n_Diago?= <germandiago@gmail.com>
Date: Mon, 20 Oct 2014 09:46:07 -0700 (PDT)
Raw View
------=_Part_1355_2000355354.1413823567860
Content-Type: text/plain; charset=UTF-8




> Though note that constexpr functions are allowed to call non-constexpr
> code,
> provided that it never reaches that condition if the input is constexpr
> and
> the variable declared constexpr too.
>

Wow, that's quite cool. Could be useful for switching between release and
non-release
versions with a config var.


> That could also be a global variable.
>
> > I think the function would have to be exposed in the header file if I am
> > right.
>
> Correct. But that's besides the point: you started with the assumption
> that
> you have the constexpr function exists.
>

I am not sure I get it. The really good thing here, actually, would be to
be able to
reuse existing code as constexpr, be it a C library or a C++ library without
previously being marked as constexpr.
But since C code cannot be constexpr I need this modification to the source,
directly, making the function constexpr + inlining the code.
Anyway, your advice helped me, thank you.


--

---
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/.

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

<div dir=3D"ltr"><br><br><blockquote class=3D"gmail_quote" style=3D"margin:=
 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><br>T=
hough note that constexpr functions are allowed to call non-constexpr code,=
=20
<br>provided that it never reaches that condition if the input is constexpr=
 and=20
<br>the variable declared constexpr too. &nbsp;<br></blockquote><div><br></=
div><div>Wow, that's quite cool. Could be useful for switching between rele=
ase and non-release</div><div>versions with a config var.</div><div>&nbsp;<=
/div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8e=
x;border-left: 1px #ccc solid;padding-left: 1ex;">That could also be a glob=
al variable.
<br>
<br>&gt; I think the function would have to be exposed in the header file i=
f I am=20
<br>&gt; right.
<br>
<br>Correct. But that's besides the point: you started with the assumption =
that=20
<br>you have the constexpr function exists.
<br></blockquote><div><br></div><div>I am not sure I get it. The really goo=
d thing here, actually, would be to be able to</div><div>reuse existing cod=
e as constexpr, be it a C library or a C++ library without</div><div>previo=
usly being marked as constexpr.</div><div>But since C code cannot be conste=
xpr I need this modification to the source,</div><div>directly, making the =
function constexpr + inlining the code.</div><div>Anyway, your advice helpe=
d me, thank you.</div><div>&nbsp;</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 />

------=_Part_1355_2000355354.1413823567860--

.