Updated C++ template documentation with respect to using a nested class as template parameter.

Fixes issue swig/swig#270.
This commit is contained in:
Michael Schaller 2015-01-05 16:43:49 +01:00
commit 007a75480a

View file

@ -3614,18 +3614,52 @@ and the second will take two integer arguments.
</p>
<p>
Needless to say, SWIG's template support provides plenty of
opportunities to break the universe. That said, an important final
point is that <b>SWIG does not perform extensive error checking of
templates!</b> Specifically, SWIG does not perform type checking nor
does it check to see if the actual contents of the template
declaration make any sense. Since the C++ compiler will hopefully
check this when it compiles the resulting wrapper file, there is no
practical reason for SWIG to duplicate this functionality (besides,
none of the SWIG developers are masochistic enough to want to
implement this right now).
Needless to say, SWIG's template support provides plenty of opportunities to
break the universe. That said, an important final point is that <b>SWIG does
not perform extensive error checking of templates!</b> Specifically, SWIG does
not perform type checking nor does it check to see if the actual contents of the
template declaration make any sense. Since the C++ compiler checks this when it
compiles the resulting wrapper file, there is no practical reason for SWIG to
duplicate this functionality.
</p>
<a name="SWIGPlus_template_nested_class_example"></a>
<p>
As SWIG's template support does not perform type checking <tt>%template</tt>
can be used as early as after a template declaration. You can, and rarely have
to, use <tt>%template</tt> before the template parameters have been declared.
For example:
</p>
<div class="code">
<pre>
template &lt;class T&gt; class OuterTemplateClass {};
// The nested class OuterClass::InnerClass inherits from the template class
// OuterTemplateClass&lt;OuterClass::InnerStruct&gt; and thus the template needs
// to be expanded with %template before the OuterClass declaration.
%template(OuterTemplateClass_OuterClass__InnerStruct)
OuterTemplateClass&lt;OuterClass::InnerStruct&gt;
// Don't forget to use %feature("flatnested") for OuterClass::InnerStruct and
// OuterClass::InnerClass if the target language doesn't support nested classes.
class OuterClass {
public:
// Forward declarations:
struct InnerStruct;
class InnerClass;
};
struct OuterClass::InnerStruct {};
// Expanding the template at this point with %template is too late as the
// OuterClass::InnerClass declaration is processed inside OuterClass.
class OuterClass::InnerClass : public OuterTemplateClass&lt;InnerStruct&gt; {};
</pre>
</div>
<p>
<b>Compatibility Note</b>: The first implementation of template support relied heavily on
macro expansion in the preprocessor. Templates have been more tightly integrated into
@ -5000,6 +5034,12 @@ class Bar {
</pre>
</div>
<p>
If a nested class has to be used as template parameter then the template might
has to be expanded before the top-level class containing the inner class gets
declared. An example can be found in the
<a href="#SWIGPlus_template_nested_class_example"> Templates</a> section.
</p>
<p>
<b>Compatibility Note:</b>