Topic: Variable templates and instantiation -- Is this well formed?


Author: Scott Prager <splinterofchaos@gmail.com>
Date: Mon, 3 Feb 2014 10:02:25 -0800 (PST)
Raw View
------=_Part_149_32281835.1391450545683
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Note: I wasn't sure whether this was best in this forum or "ISO C++=20
Standard", so please forgive me if I should have posted there.

Given the example, *pi*, from N3651<http://www.google.com/url?sa=3Dt&rct=3D=
j&q=3D&esrc=3Ds&source=3Dweb&cd=3D1&cad=3Drja&ved=3D0CCYQFjAA&url=3Dhttp%3A=
%2F%2Fisocpp.org%2Ffiles%2Fpapers%2FN3651.pdf&ei=3DisTvUpDzIo6MyAGau4GYCg&u=
sg=3DAFQjCNHOwaj3WD33nIpo0eMrc29jjI605w&sig2=3DoMEPanN2jNhlvwAJamkYZA&bvm=
=3Dbv.60444564,d.aWc>,=20
is this code well formed?

template<typename T>
constexpr T pi =3D T(3.1415926535897932385);

template<typename T>
auto area( T r ) {
    return pi<T> * r * r;
};

int main() {
    pi<double>;
    float a =3D area(10.f);
    return a;
}



When I compile, clang spits out

test2.cpp:10:11: warning: unused variable 'a' [-Wunused-variable]
    float a =3D area(10.f);
          ^
test2.cpp:2:13: warning: variable 'pi<float>' has internal linkage but is=
=20
not defined
      [-Wundefined-internal]
constexpr T pi =3D T(3.1415926535897932385);
            ^
test2.cpp:6:12: note: used here
    return pi<T> * r * r;
           ^
2 warnings generated.
/tmp/test2-e8896e.o: In function `auto area<float>(float)':
test2.cpp:(.text._Z4areaIfEDaT_[_Z4areaIfEDaT_]+0xe): undefined reference=
=20
to `_ZL2piIfE'
clang: error: linker command failed with exit code 1 (use -v to see=20
invocation)
fish: Unknown command =E2=80=9C./a.out=E2=80=9D


The one thing that makes this work is when I add an explicit instantiation=
=20
for *pi<float>* like so:

    template<> float pi<float> =3D (float)pi<double>;

which seems to defeat the purpose entirely.

I don't exactly understand the text of the proposal (particularly in the=20
section Modifications to the standard text), but it says (adding to section=
=20
14/6) that a variable template (among other things) "..shall be defined in=
=20
every translation unit in which it is implicitly instantiated". So unless I=
=20
am misunderstanding something, the *pi<T>* in my example implicitly=20
instantiated *pi<float>* when I called *area* with a *float*, but clang did=
=20
not define it, meaning the problem lies with clang itself. Is this correct,=
=20
or is there an issue with the proposal's wording?

--=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_149_32281835.1391450545683
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Note: I wasn't sure whether this was best in this forum or=
 "ISO C++ Standard", so please forgive me if I should have posted there.<br=
><br>Given the example, <i>pi</i>, from <a href=3D"http://www.google.com/ur=
l?sa=3Dt&amp;rct=3Dj&amp;q=3D&amp;esrc=3Ds&amp;source=3Dweb&amp;cd=3D1&amp;=
cad=3Drja&amp;ved=3D0CCYQFjAA&amp;url=3Dhttp%3A%2F%2Fisocpp.org%2Ffiles%2Fp=
apers%2FN3651.pdf&amp;ei=3DisTvUpDzIo6MyAGau4GYCg&amp;usg=3DAFQjCNHOwaj3WD3=
3nIpo0eMrc29jjI605w&amp;sig2=3DoMEPanN2jNhlvwAJamkYZA&amp;bvm=3Dbv.60444564=
,d.aWc">N3651</a>, is this code well formed?<br><br><div class=3D"prettypri=
nt" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 1=
87, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"><=
code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"col=
or: #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">typename</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">constexpr</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> T pi </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 T</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>=
<span style=3D"color: #066;" class=3D"styled-by-prettify">3.141592653589793=
2385</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span=
><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 s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">auto</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> area</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> T r </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">return</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> pi</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">&gt;</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"> r </span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">*</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> r</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"colo=
r: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> main</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br>&nbsp; &nbsp; pi</span><span style=3D"color: #080;" class=3D"styled-by-p=
rettify">&lt;double&gt;</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>&nbsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">float</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> a </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> area</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #066;" class=3D"styled-by-prettify">10.f</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> a</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" cl=
ass=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-b=
y-prettify"><br></span></div></code></div><br><br><br>When I compile, clang=
 spits out<br><div class=3D"prettyprint" style=3D"background-color: rgb(250=
, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-=
width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br>test2</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">cpp</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span =
style=3D"color: #066;" class=3D"styled-by-prettify">10</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #=
066;" class=3D"styled-by-prettify">11</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">:</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> warning</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">:</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> unused variable </span><span style=3D"color: #080;" class=3D"styled=
-by-prettify">'a'</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">[-=
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Wunused</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">-</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">variable</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">]</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">float</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> a </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> area</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><span style=3D"color: #066;" class=3D"s=
tyled-by-prettify">10.f</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">^</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br>test2</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">cpp</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">:</span><span style=3D"color: #066;" class=3D"styled-by-prettify">2=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><s=
pan style=3D"color: #066;" class=3D"styled-by-prettify">13</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> warning</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> variable </span><span style=3D"color: #080;" c=
lass=3D"styled-by-prettify">'pi&lt;float&gt;'</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> has </span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">internal</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> linkage but </span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">is</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">not</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">d=
efined</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
&nbsp; &nbsp; &nbsp; </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">[-</span><span style=3D"color: #606;" class=3D"styled-by-prettif=
y">Wundefined</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">-</span><span style=3D"color: #008;" class=3D"styled-by-prettify">intern=
al</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">constexpr</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> T pi </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #066;" class=
=3D"styled-by-prettify">3.1415926535897932385</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">^</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br>test2</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">cpp</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #066;" c=
lass=3D"styled-by-prettify">6</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">:</span><span style=3D"color: #066;" class=3D"styled-by-=
prettify">12</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> note</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> used here<br>&nbsp; =
&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">ret=
urn</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> pi</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">T</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"co=
lor: #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"> r </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">*</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp=
; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">^</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br></span><span style=3D"color: #066;" class=3D"styled-by=
-prettify">2</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> warnings generated</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"styled-by-prettify">/</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">tmp</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">/</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">test2</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">-</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">e8896e</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">o</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: #606;" class=3D"styled-by-prettify">In</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">function</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #080;" class=3D"styled-by-prettify">`auto area&lt;float&gt;(float)':<br=
>test2.cpp:(.text._Z4areaIfEDaT_[_Z4areaIfEDaT_]+0xe): undefined reference =
to `</span><span style=3D"color: #000;" class=3D"styled-by-prettify">_ZL2pi=
IfE</span><span style=3D"color: #080;" class=3D"styled-by-prettify">'<br>cl=
ang: error: linker command failed with exit code 1 (use -v to see invocatio=
n)<br>fish: Unknown command =E2=80=9C./a.out=E2=80=9D<br><br></span></div><=
/code></div><br>The one thing that makes this work is when I add an explici=
t instantiation for <i>pi&lt;float&gt;</i> like so:<br><br>&nbsp;&nbsp;&nbs=
p; template&lt;&gt; float pi&lt;float&gt; =3D (float)pi&lt;double&gt;;<br><=
br>which seems to defeat the purpose entirely.<br><br>I don't exactly under=
stand the text of the proposal (particularly in the section Modifications t=
o the standard text), but it says (adding to section 14/6) that a variable =
template (among other things) "..shall be defined in every translation unit=
 in which it is implicitly instantiated". So unless I am misunderstanding s=
omething, the <i>pi&lt;T&gt;</i> in my example implicitly instantiated <i>p=
i&lt;float&gt;</i> when I called <i>area</i> with a <i>float</i>, but clang=
 did not define it, meaning the problem lies with clang itself. Is this cor=
rect, or is there an issue with the proposal's wording?<br></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_149_32281835.1391450545683--

.