<!DOCTYPE html><html lang='en'><head><title>[temp.class.spec.mfunc]</title><meta charset='UTF-8'/><link rel='stylesheet' type='text/css' href='14882.css'/><link rel='stylesheet' type='text/css' href='expanded.css' title='Notes and examples expanded'/><link rel='alternate stylesheet' type='text/css' href='colored.css' title='Notes and examples colored'/><link rel='alternate stylesheet' type='text/css' href='collapsed.css' title='Notes and examples collapsed'/><link rel='icon' href='icon.png'/></head><body><div class='wrapper'><h1 ><a class='secnum' style='min-width:73pt'>12</a> Templates <a class='abbr_ref' href='./#temp'>[temp]</a></h1><h2 ><a class='secnum' style='min-width:88pt'>12.6</a> Template declarations <a class='abbr_ref' href='temp.decls#temp.class.spec.mfunc'>[temp.decls]</a></h2><h3 ><a class='secnum' style='min-width:103pt'>12.6.5</a> Class template partial specializations <a class='abbr_ref' href='temp.class.spec#mfunc'>[temp.class.spec]</a></h3><h4 ><a class='secnum' style='min-width:118pt'>12.6.5.3</a> Members of class template specializations <a class='abbr_ref'>[temp.class.spec.mfunc]</a></h4><div class='para' id='1'><div class='marginalizedparent'><a class='marginalized' href='#1'>1</a></div><div class='sourceLinkParent'><a class='sourceLink' href='https://github.com/Eelis/draft73ca144c7e48612fe72742f9f6a5ec9ad6a934d2/source/templates.tex#L3318'>#</a></div><div id='1.sentence-1' class='sentence'>The template parameter list of a member of a class template partial
specialization shall match the template parameter list of the class template
partial specialization<a class='hidden_link' href='#1.sentence-1'>.</a></div> <div id='1.sentence-2' class='sentence'>The template argument list of a member of a class template partial
specialization shall match the template argument list of the class template
partial specialization<a class='hidden_link' href='#1.sentence-2'>.</a></div> <div id='1.sentence-3' class='sentence'>A class template specialization is a distinct template<a class='hidden_link' href='#1.sentence-3'>.</a></div> <div id='1.sentence-4' class='sentence'>The members of the class template partial specialization are
unrelated to the members of the primary template<a class='hidden_link' href='#1.sentence-4'>.</a></div> <div id='1.sentence-5' class='sentence'>Class template partial specialization members that are used in a way that
requires a definition shall be defined; the definitions of members of the
primary template are never used as definitions for members of a class
template partial specialization<a class='hidden_link' href='#1.sentence-5'>.</a></div> <div id='1.sentence-6' class='sentence'>An explicit specialization of a member of a class template partial
specialization is declared in the same way as an explicit specialization of
the primary template<a class='hidden_link' href='#1.sentence-6'>.</a></div> <div id='1.example-1' class='example'>[<span style='white-space:nowrap'> </span><a class='example_link' href='#1.example-1'><span class='textit'>Example</span></a><div class='exampleBody'><span class='textit'>:</span> <pre class='codeblock'>
<span class='comment'>// primary class template</span>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T, <span class='keyword'>int</span> I<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> A <span class='curlybracket'>{</span>
  <span class='keyword'>void</span> f<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;
<span class='curlybracket'>}</span>;

<span class='comment'>// member of primary class template</span>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T, <span class='keyword'>int</span> I<span class='anglebracket'>&gt;</span> <span class='keyword'>void</span> A<span class='anglebracket'>&lt;</span>T,I<span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>f<span class='parenthesis'>(</span><span class='parenthesis'>)</span> <span class='curlybracket'>{</span> <span class='curlybracket'>}</span>

<span class='comment'>// class template partial specialization</span>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> A<span class='anglebracket'>&lt;</span>T,<span class='literal'>2</span><span class='anglebracket'>&gt;</span> <span class='curlybracket'>{</span>
  <span class='keyword'>void</span> f<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;
  <span class='keyword'>void</span> g<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;
  <span class='keyword'>void</span> h<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;
<span class='curlybracket'>}</span>;

<span class='comment'>// member of class template partial specialization</span>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T<span class='anglebracket'>&gt;</span> <span class='keyword'>void</span> A<span class='anglebracket'>&lt;</span>T,<span class='literal'>2</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>g<span class='parenthesis'>(</span><span class='parenthesis'>)</span> <span class='curlybracket'>{</span> <span class='curlybracket'>}</span>

<span class='comment'>// explicit specialization</span>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='anglebracket'>&gt;</span> <span class='keyword'>void</span> A<span class='anglebracket'>&lt;</span><span class='keyword'>char</span>,<span class='literal'>2</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>h<span class='parenthesis'>(</span><span class='parenthesis'>)</span> <span class='curlybracket'>{</span> <span class='curlybracket'>}</span>

<span class='keyword'>int</span> main<span class='parenthesis'>(</span><span class='parenthesis'>)</span> <span class='curlybracket'>{</span>
  A<span class='anglebracket'>&lt;</span><span class='keyword'>char</span>,<span class='literal'>0</span><span class='anglebracket'>&gt;</span> a0;
  A<span class='anglebracket'>&lt;</span><span class='keyword'>char</span>,<span class='literal'>2</span><span class='anglebracket'>&gt;</span> a2;
  a0<span class='operator'>.</span>f<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;           <span class='comment'>// OK, uses definition of primary template's member</span>
  a2<span class='operator'>.</span>g<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;           <span class='comment'>// OK, uses definition of partial specialization's member</span>
  a2<span class='operator'>.</span>h<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;           <span class='comment'>// OK, uses definition of explicit specialization's member</span>
  a2<span class='operator'>.</span>f<span class='parenthesis'>(</span><span class='parenthesis'>)</span>;           <span class='comment'>// ill-formed, no definition of <span class='tcode_in_codeblock'>f</span> for <span class='tcode_in_codeblock'>A&lt;T,2&gt;</span>; the primary template is not used here</span>
<span class='curlybracket'>}</span>
</pre> —<span style='white-space:nowrap'> </span><i>end example</i></div><span style='white-space:nowrap'> </span>]</div> </div><div class='para' id='2'><div class='marginalizedparent'><a class='marginalized' href='#2'>2</a></div><div class='sourceLinkParent'><a class='sourceLink' href='https://github.com/Eelis/draft73ca144c7e48612fe72742f9f6a5ec9ad6a934d2/source/templates.tex#L3370'>#</a></div><div id='2.sentence-1' class='sentence'>If a member template of a class template is partially specialized,
the member template partial specializations are member templates of
the enclosing class template;
if the enclosing class template is instantiated (<a href='temp.inst'>[temp.inst]</a>, <a href='temp.explicit'>[temp.explicit]</a>),
a declaration for every member template partial specialization is also
instantiated as part of creating the members of the class template
specialization<a class='hidden_link' href='#2.sentence-1'>.</a></div> <div id='2.sentence-2' class='sentence'>If the primary member template is explicitly specialized for a given
(implicit) specialization of the enclosing class template,
the partial specializations of the member template are ignored for this
specialization of the enclosing class template<a class='hidden_link' href='#2.sentence-2'>.</a></div> <div id='2.sentence-3' class='sentence'>If a partial specialization of the member template is explicitly specialized
for a given (implicit) specialization of the enclosing class template,
the primary member template and its other partial specializations are
still considered for this specialization of the enclosing class template<a class='hidden_link' href='#2.sentence-3'>.</a></div> <div id='2.example-1' class='example'>[<span style='white-space:nowrap'> </span><a class='example_link' href='#2.example-1'><span class='textit'>Example</span></a><div class='exampleBody'><span class='textit'>:</span> <pre class='codeblock'>
<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> A <span class='curlybracket'>{</span>
  <span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T2<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> B <span class='curlybracket'>{</span><span class='curlybracket'>}</span>;                     <span class='comment'>// #1</span>
  <span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T2<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> B<span class='anglebracket'>&lt;</span>T2<span class='operator'>*</span><span class='anglebracket'>&gt;</span> <span class='curlybracket'>{</span><span class='curlybracket'>}</span>;                <span class='comment'>// #2</span>
<span class='curlybracket'>}</span>;

<span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='anglebracket'>&gt;</span> <span class='keyword'>template</span><span class='anglebracket'>&lt;</span><span class='keyword'>class</span> T2<span class='anglebracket'>&gt;</span> <span class='keyword'>struct</span> A<span class='anglebracket'>&lt;</span><span class='keyword'>short</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>B <span class='curlybracket'>{</span><span class='curlybracket'>}</span>;  <span class='comment'>// #3</span>

A<span class='anglebracket'>&lt;</span><span class='keyword'>char</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>B<span class='anglebracket'>&lt;</span><span class='keyword'>int</span><span class='operator'>*</span><span class='anglebracket'>&gt;</span>  abcip;                              <span class='comment'>// uses #2</span>
A<span class='anglebracket'>&lt;</span><span class='keyword'>short</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>B<span class='anglebracket'>&lt;</span><span class='keyword'>int</span><span class='operator'>*</span><span class='anglebracket'>&gt;</span> absip;                              <span class='comment'>// uses #3</span>
A<span class='anglebracket'>&lt;</span><span class='keyword'>char</span><span class='anglebracket'>&gt;</span><span class='operator'>:</span><span class='operator'>:</span>B<span class='anglebracket'>&lt;</span><span class='keyword'>int</span><span class='anglebracket'>&gt;</span>  abci;                                <span class='comment'>// uses #1</span>
</pre> —<span style='white-space:nowrap'> </span><i>end example</i></div><span style='white-space:nowrap'> </span>]</div> </div></div></body></html>