Topic: Subsets of constexpr functions should be considered


Author: Mark <turck11@gmail.com>
Date: Fri, 6 Dec 2013 20:31:43 -0800 (PST)
Raw View
------=_Part_1191_17579079.1386390703795
Content-Type: text/plain; charset=ISO-8859-1

I've been toying around with the relaxed constexpr rules of C++14 in an SVN
build of clang. The thought occurred to me that I might be able to use the
'natural' syntax of a for loop to iterate over the contents in a std::tuple
at compile-time in a constexpr function. This seems conceptually plausible,
as I am now allowed to loop over a range at compile-time, but as I found
out on Stack Overflow<http://stackoverflow.com/questions/20436611/possible-to-instantiate-templates-using-a-for-loop-in-a-c14-constexpr-function>,
when speaking strict standardese, I cannot instantiate a template (like
std::get) with my loop variable because the variable is not constexpr, even
though it is part of a constexpr function.

A simplified example:

template<int N>
constexpr int foo() {
  return N;
}

constexpr int getSum() {
  auto sum = 0;
  for (auto i = 0; i < 10; ++i) {
    sum += foo<i>(); //The interesting part
  }
  return sum;
}

constexpr auto sum = getSum();




I'd like to open discussion as to whether or not it makes sense to allow
this.


I should also clarify, I understand that constexpr functions can be invoked
at runtime. I am not talking about that case; there are already cases where
constexpr functions fail to compile only when used for runtime invocations,
I would extend that principal to this case. I only propose this relaxation
for when a constexpr function is evaluated in a compile-time context.

--

---
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_1191_17579079.1386390703795
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I've been toying around with the relaxed constexpr ru=
les of C++14 in an SVN build of clang. The thought occurred to me that I mi=
ght be able to use the 'natural' syntax of a for loop to iterate over the c=
ontents in a std::tuple at compile-time in a constexpr function. This seems=
 conceptually plausible, as I am now allowed to loop over a range at compil=
e-time, but as I found out on<a href=3D"http://stackoverflow.com/questions/=
20436611/possible-to-instantiate-templates-using-a-for-loop-in-a-c14-conste=
xpr-function"> Stack Overflow</a>, when speaking strict standardese, I cann=
ot instantiate a template (like std::get) with my loop variable because the=
 variable is not constexpr, even though it is part of a constexpr function.=
<br></div><div><br></div><div><div>A simplified example:</div><div><div cla=
ss=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border: 1=
px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D"prettyp=
rint"><div class=3D"subprettyprint"><pre class=3D"lang-c prettyprint pretty=
printed" style=3D"margin-bottom: 10px; padding: 5px; font-size: 14px; backg=
round-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Luc=
ida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans M=
ono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-hei=
ght: 600px; word-wrap: normal; color: rgb(0, 0, 0); line-height: 18px;"><co=
de style=3D"font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Libera=
tion Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', =
monospace, serif;"><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">int</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> N</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">constexpr</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> foo</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nb=
sp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> N</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">constexpr</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> getSum</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> sum </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #0=
66;" class=3D"styled-by-prettify">0</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br>&nbsp; </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">for</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> i </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #066;" class=3D"styled-by-prettify">0</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> i </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">10=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">++</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">i</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br>&nbsp; &nbsp; sum </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">+=3D</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> foo</span><span style=3D"color: #080;" class=3D"styled-by-prettif=
y">&lt;i&gt;</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">();</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #800;" class=3D"styled-by-prettify">//The interest=
ing part</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r>&nbsp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">}=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp;=
 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> sum</span><s=
pan 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"co=
lor: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">constexpr</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">auto</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> sum </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> getSum</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
);</span></code></pre></div></code></div><br><br><div><br></div><div>I'd li=
ke to open discussion as to whether or not it makes sense to allow this.</d=
iv></div></div><div><br></div><div><br></div><div>I should also clarify, I =
understand that constexpr functions can be invoked at runtime. I am not tal=
king about that case; there are already cases where constexpr functions fai=
l to compile only when used for runtime invocations, I would extend that pr=
incipal to this case. I only propose this relaxation for when a constexpr f=
unction is evaluated in a compile-time context.</div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_1191_17579079.1386390703795--

.