From 007a75480aa92156784825a1cf5dd24f97397e0b Mon Sep 17 00:00:00 2001 From: Michael Schaller Date: Mon, 5 Jan 2015 16:43:49 +0100 Subject: [PATCH 1/2] Updated C++ template documentation with respect to using a nested class as template parameter. Fixes issue swig/swig#270. --- Doc/Manual/SWIGPlus.html | 60 +++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/Doc/Manual/SWIGPlus.html b/Doc/Manual/SWIGPlus.html index 62c0e8d1e..c1ca5e1d3 100644 --- a/Doc/Manual/SWIGPlus.html +++ b/Doc/Manual/SWIGPlus.html @@ -3614,18 +3614,52 @@ and the second will take two integer arguments.

-Needless to say, SWIG's template support provides plenty of -opportunities to break the universe. That said, an important final -point is that SWIG does not perform extensive error checking of -templates! 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 SWIG does +not perform extensive error checking of templates! 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.

+ +

+As SWIG's template support does not perform type checking %template +can be used as early as after a template declaration. You can, and rarely have +to, use %template before the template parameters have been declared. +For example: +

+ +
+
+template <class T> class OuterTemplateClass {};
+
+// The nested class OuterClass::InnerClass inherits from the template class
+// OuterTemplateClass<OuterClass::InnerStruct> and thus the template needs
+// to be expanded with %template before the OuterClass declaration.
+%template(OuterTemplateClass_OuterClass__InnerStruct)
+    OuterTemplateClass<OuterClass::InnerStruct>
+
+
+// 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<InnerStruct> {};
+
+
+

Compatibility Note: 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 { +

+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 + Templates section. +

Compatibility Note: From f25f5cf635396c581507ee932b877bb2c202b584 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 16 Jan 2015 19:08:41 +0000 Subject: [PATCH 2/2] Nested class template doc tweaks --- Doc/Manual/SWIGPlus.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/Manual/SWIGPlus.html b/Doc/Manual/SWIGPlus.html index c1ca5e1d3..eeca0291c 100644 --- a/Doc/Manual/SWIGPlus.html +++ b/Doc/Manual/SWIGPlus.html @@ -5035,10 +5035,10 @@ class Bar {

-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 - Templates section. +If a nested class, within an outer class, has to be used as a template parameter within the outer class, then the template will +have to be instantiated with %template before the beginning of the outer class. +An example can be found in the +Templates section.