Topic: Explicitly defining template class members


Author: tmlen <timlenertz@gmail.com>
Date: Thu, 26 Feb 2015 05:45:49 -0800 (PST)
Raw View
------=_Part_4_584717252.1424958349649
Content-Type: multipart/alternative;
 boundary="----=_Part_5_457685692.1424958349649"

------=_Part_5_457685692.1424958349649
Content-Type: text/plain; charset=UTF-8

Given a class template like:

template<typename T>
class a {
public:
    void f();
};

template<typename T>
void a<T>::f() {
    ...
}

C++11 has explicit class template instantiation declarations:

extern template class a<int>;

To define that a translation unit encountering a<int> should not try to
instantiate it, but instead look for an existing (explicit) instantiation
to link to. In a shared library, this corresponding template instantiation
definition would be put in a module of the library:

template class a<int>;

This is useful for example when the shared library is compiled with more
compiler optimization, with OpenMP, ... than the program using it.

But currently, template class a<int>; does not instantiate the template
class's member function definitions (only the declarations). So to also
include an instantiation of a<int>::f this would be needed: In the header:

extern template class a<int>;
extern void a<int>::f();

and in the source

template class a<int>;
void a<int>::f();

It may be useful to be able to state in the template class definition that
when the class template gets explicitly instantiated, some of its member
functions, or other related classes/functions should get too. Something like

template<typename T>
class a {
public:
    using helper = helper<T>;
    void f();
    template<typename U> void g();

    extern helper;
    extern f();
    extern g<int>();
    extern g<long>();
};

--

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

<div dir=3D"ltr"><div>Given a class template like:</div><div><br></div><div=
><font face=3D"courier new, monospace">template&lt;typename T&gt;</font></d=
iv><div><font face=3D"courier new, monospace">class a {</font></div><div><f=
ont face=3D"courier new, monospace">public:</font></div><div><font face=3D"=
courier new, monospace">&nbsp; &nbsp; void f();</font></div><div><font face=
=3D"courier new, monospace">};</font></div><div><font face=3D"courier new, =
monospace"><br></font></div><div><font face=3D"courier new, monospace">temp=
late&lt;typename T&gt;</font></div><div><font face=3D"courier new, monospac=
e">void a&lt;T&gt;::f() {</font></div><div><font face=3D"courier new, monos=
pace">&nbsp; &nbsp; ...</font></div><div><font face=3D"courier new, monospa=
ce">}</font></div><div><br></div>C++11 has explicit class template instanti=
ation declarations:<div><br></div><div><font face=3D"courier new, monospace=
">extern template class a&lt;int&gt;;</font></div><div><br></div><div>To de=
fine that a translation unit encountering <font face=3D"courier new, monosp=
ace">a&lt;int&gt;</font> should not try to instantiate it, but instead look=
 for an existing (explicit) instantiation to link to. In a shared library, =
this corresponding template instantiation definition would be put in a modu=
le of the library:</div><div><br></div><div><font face=3D"courier new, mono=
space">template class a&lt;int&gt;;<br></font></div><div><br></div><div>Thi=
s is useful for example when the shared library is compiled with more compi=
ler optimization, with OpenMP, ... than the program using it.</div><div><br=
></div><div>But currently, <font face=3D"courier new, monospace">template c=
lass a&lt;int&gt;;</font> does not instantiate the template class's member =
function definitions (only the declarations). So to also include an instant=
iation of <font face=3D"courier new, monospace">a&lt;int&gt;::f</font> this=
 would be needed: In the header:</div><div><br></div><div><font face=3D"cou=
rier new, monospace">extern template class a&lt;int&gt;;<br></font></div><d=
iv><font face=3D"courier new, monospace">extern void a&lt;int&gt;::f();<br>=
</font></div><div><br></div><div>and in the source</div><div><br></div><div=
><div><font face=3D"courier new, monospace">template class a&lt;int&gt;;</f=
ont></div><div><font face=3D"courier new, monospace">void a&lt;int&gt;::f()=
;</font></div></div><div><br></div><div>It may be useful to be able to stat=
e in the template class definition that when the class template gets explic=
itly instantiated, some of its member functions, or other related classes/f=
unctions should get too. Something like</div><div><br></div><div><div><font=
 face=3D"courier new, monospace">template&lt;typename T&gt;</font></div><di=
v><font face=3D"courier new, monospace">class a {</font></div><div><font fa=
ce=3D"courier new, monospace">public:</font></div><div><font face=3D"courie=
r new, monospace">&nbsp; &nbsp; using helper =3D helper&lt;T&gt;;</font></d=
iv><div><font face=3D"courier new, monospace">&nbsp; &nbsp; void f();</font=
></div><div><font face=3D"courier new, monospace">&nbsp; &nbsp; template&lt=
;typename U&gt; void g();</font></div><div><font face=3D"courier new, monos=
pace"><br></font></div><div><font face=3D"courier new, monospace">&nbsp; &n=
bsp; extern helper;</font></div><div><font face=3D"courier new, monospace">=
&nbsp; &nbsp; extern f();</font></div><div><font face=3D"courier new, monos=
pace">&nbsp; &nbsp; extern g&lt;int&gt;();</font></div><div><font face=3D"c=
ourier new, monospace">&nbsp; &nbsp; extern g&lt;long&gt;();</font></div><d=
iv><font face=3D"courier new, monospace">};</font></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 />

------=_Part_5_457685692.1424958349649--
------=_Part_4_584717252.1424958349649--

.


Author: =?UTF-8?Q?David_Rodr=C3=ADguez_Ibeas?= <dibeas@ieee.org>
Date: Thu, 26 Feb 2015 20:18:06 +0000
Raw View
--001a1133dde22b56c50510037316
Content-Type: text/plain; charset=UTF-8

> But currently, template class a<int>; does not instantiate the template
class's member function definitions (only the declarations).

14.7.2/8
An explicit instantiation that names a class template specialization is
also an explicit instantiation of the
same kind (declaration or definition) of each of its members (not including
members inherited from base
classes and members that are templates) that has not been previously
explicitly specialized in the translation
unit containing the explicit instantiation, except as described below.

Am I misunderstanding the issue? The explicit instantiation of the class
*does* explicitly instantiate the members.

    David

On Thu, Feb 26, 2015 at 8:45 AM tmlen <timlenertz@gmail.com> wrote:

> Given a class template like:
>
> template<typename T>
> class a {
> public:
>     void f();
> };
>
> template<typename T>
> void a<T>::f() {
>     ...
> }
>
> C++11 has explicit class template instantiation declarations:
>
> extern template class a<int>;
>
> To define that a translation unit encountering a<int> should not try to
> instantiate it, but instead look for an existing (explicit) instantiation
> to link to. In a shared library, this corresponding template instantiation
> definition would be put in a module of the library:
>
> template class a<int>;
>
> This is useful for example when the shared library is compiled with more
> compiler optimization, with OpenMP, ... than the program using it.
>
> But currently, template class a<int>; does not instantiate the template
> class's member function definitions (only the declarations). So to also
> include an instantiation of a<int>::f this would be needed: In the header:
>
> extern template class a<int>;
> extern void a<int>::f();
>
> and in the source
>
> template class a<int>;
> void a<int>::f();
>
> It may be useful to be able to state in the template class definition that
> when the class template gets explicitly instantiated, some of its member
> functions, or other related classes/functions should get too. Something like
>
> template<typename T>
> class a {
> public:
>     using helper = helper<T>;
>     void f();
>     template<typename U> void g();
>
>     extern helper;
>     extern f();
>     extern g<int>();
>     extern g<long>();
> };
>
> --
>
> ---
> 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/.

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

<div dir=3D"ltr">&gt;=C2=A0<span style=3D"font-size:13.1999998092651px;line=
-height:19.7999992370605px">But currently,=C2=A0</span><font face=3D"courie=
r new, monospace" style=3D"font-size:13.1999998092651px;line-height:19.7999=
992370605px">template class a&lt;int&gt;;</font><span style=3D"font-size:13=
..1999998092651px;line-height:19.7999992370605px">=C2=A0does not instantiate=
 the template class&#39;s member function definitions (only the declaration=
s).<br></span><br>14.7.2/8<br><div>An explicit instantiation that names a c=
lass template specialization is also an explicit instantiation of the</div>=
<div>same kind (declaration or definition) of each of its members (not incl=
uding members inherited from base</div><div>classes and members that are te=
mplates) that has not been previously explicitly specialized in the transla=
tion</div><div>unit containing the explicit instantiation, except as descri=
bed below.<br><br>Am I misunderstanding the issue? The explicit instantiati=
on of the class *does* explicitly instantiate the members.<br><br>=C2=A0 =
=C2=A0 David</div></div><br><div class=3D"gmail_quote">On Thu, Feb 26, 2015=
 at 8:45 AM tmlen &lt;<a href=3D"mailto:timlenertz@gmail.com">timlenertz@gm=
ail.com</a>&gt; wrote:<br><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>Given a class template like:</div><div><br></div><div><font face=3D"cou=
rier new, monospace">template&lt;typename T&gt;</font></div><div><font face=
=3D"courier new, monospace">class a {</font></div><div><font face=3D"courie=
r new, monospace">public:</font></div><div><font face=3D"courier new, monos=
pace">=C2=A0 =C2=A0 void f();</font></div><div><font face=3D"courier new, m=
onospace">};</font></div><div><font face=3D"courier new, monospace"><br></f=
ont></div><div><font face=3D"courier new, monospace">template&lt;typename T=
&gt;</font></div><div><font face=3D"courier new, monospace">void a&lt;T&gt;=
::f() {</font></div><div><font face=3D"courier new, monospace">=C2=A0 =C2=
=A0 ...</font></div><div><font face=3D"courier new, monospace">}</font></di=
v><div><br></div>C++11 has explicit class template instantiation declaratio=
ns:<div><br></div><div><font face=3D"courier new, monospace">extern templat=
e class a&lt;int&gt;;</font></div><div><br></div><div>To define that a tran=
slation unit encountering <font face=3D"courier new, monospace">a&lt;int&gt=
;</font> should not try to instantiate it, but instead look for an existing=
 (explicit) instantiation to link to. In a shared library, this correspondi=
ng template instantiation definition would be put in a module of the librar=
y:</div><div><br></div><div><font face=3D"courier new, monospace">template =
class a&lt;int&gt;;<br></font></div><div><br></div><div>This is useful for =
example when the shared library is compiled with more compiler optimization=
, with OpenMP, ... than the program using it.</div><div><br></div><div>But =
currently, <font face=3D"courier new, monospace">template class a&lt;int&gt=
;;</font> does not instantiate the template class&#39;s member function def=
initions (only the declarations). So to also include an instantiation of <f=
ont face=3D"courier new, monospace">a&lt;int&gt;::f</font> this would be ne=
eded: In the header:</div><div><br></div><div><font face=3D"courier new, mo=
nospace">extern template class a&lt;int&gt;;<br></font></div><div><font fac=
e=3D"courier new, monospace">extern void a&lt;int&gt;::f();<br></font></div=
><div><br></div><div>and in the source</div><div><br></div><div><div><font =
face=3D"courier new, monospace">template class a&lt;int&gt;;</font></div><d=
iv><font face=3D"courier new, monospace">void a&lt;int&gt;::f();</font></di=
v></div><div><br></div><div>It may be useful to be able to state in the tem=
plate class definition that when the class template gets explicitly instant=
iated, some of its member functions, or other related classes/functions sho=
uld get too. Something like</div><div><br></div><div><div><font face=3D"cou=
rier new, monospace">template&lt;typename T&gt;</font></div><div><font face=
=3D"courier new, monospace">class a {</font></div><div><font face=3D"courie=
r new, monospace">public:</font></div><div><font face=3D"courier new, monos=
pace">=C2=A0 =C2=A0 using helper =3D helper&lt;T&gt;;</font></div><div><fon=
t face=3D"courier new, monospace">=C2=A0 =C2=A0 void f();</font></div><div>=
<font face=3D"courier new, monospace">=C2=A0 =C2=A0 template&lt;typename U&=
gt; void g();</font></div><div><font face=3D"courier new, monospace"><br></=
font></div><div><font face=3D"courier new, monospace">=C2=A0 =C2=A0 extern =
helper;</font></div><div><font face=3D"courier new, monospace">=C2=A0 =C2=
=A0 extern f();</font></div><div><font face=3D"courier new, monospace">=C2=
=A0 =C2=A0 extern g&lt;int&gt;();</font></div><div><font face=3D"courier ne=
w, monospace">=C2=A0 =C2=A0 extern g&lt;long&gt;();</font></div><div><font =
face=3D"courier new, monospace">};</font></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" 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>
</blockquote></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 />

--001a1133dde22b56c50510037316--

.