Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you overload templated function (specialized) in child class?
    text
    copied!<p>I have a base class with a templated function that has the general templated type, as well as specialized version. </p> <pre><code>#ifndef BASE_CLASS #define BASE_CLASS #include &lt;iostream&gt; using namespace std; struct Type1 { }; struct Type2 { }; class baseClass { public: template&lt;class Type&gt; void doStuff(Type &amp; t) { templateFunction(t); } template&lt;class Type&gt; void templateFunction(Type &amp; t); }; template&lt;class Type&gt; void baseClass::templateFunction(Type &amp; t) { cout &lt;&lt; "This is the generic function!" &lt;&lt; endl; } template&lt;&gt; void baseClass::templateFunction(Type1 &amp; t) { cout &lt;&lt; "This is the specialized function: - Type1" &lt;&lt; endl; } #endif </code></pre> <p>I also have a child class, that inherits from "baseClass". However, the child class requires different functionality for that specialization.</p> <pre><code>#ifndef CHILD_CLASS #define CHILD_CLASS #include "BaseClass.h" class ChildClass : public baseClass { public: }; template&lt;&gt; void ChildClass::templateFunction(Type1 &amp; t) { cout &lt;&lt; "We overloaded the specialized template function for type 1!" &lt;&lt; endl; } #endif </code></pre> <p>The above does not compile:</p> <p>ChildClass.h:13: error: no member function âtemplateFunctionâ declared in âChildClassâ ChildClass.h:13: error: invalid function declaration</p> <p>If I change the "overloaded" function to: </p> <pre><code>template&lt;&gt; void baseClass::templateFunction(Type1 &amp; t) { cout &lt;&lt; "We overloaded the specialized template function for type 1!" &lt;&lt; endl; } </code></pre> <p>I get: ChildClass.h:13: error: redefinition of âvoid baseClass::templateFunction(Type&amp;) [with Type = Type1]â BaseClass.h:36: error: âvoid baseClass::templateFunction(Type&amp;) [with Type = Type1]â previously declared here</p> <p><strong>How do I properly overload specialized template functions in child classes?</strong></p> <p>For reference, the main:</p> <pre><code>#include "BaseClass.h" #include "ChildClass.h" int main() { Type1 first; Type2 second; baseClass theBaseClass; ChildClass theChildClass; theBaseClass.doStuff(first); theBaseClass.doStuff(second); theChildClass.doStuff(first); theChildClass.doStuff(second); return 0; } </code></pre> <p>On the suggestion of: Kerrek SB, I've changed the ChildClass to:</p> <pre><code>#ifndef CHILD_CLASS #define CHILD_CLASS #include "BaseClass.h" class ChildClass : public baseClass { public: template&lt;class Type&gt; void templateFunction(Type &amp; t); }; template&lt;&gt; void ChildClass::templateFunction(Type1 &amp; t) { cout &lt;&lt; "We overloaded the specialized template function for type 1!" &lt;&lt; endl; } #endif </code></pre> <p>The output: </p> <pre><code>This is the specialized function: - Type1 This is the generic function! This is the specialized function: - Type1 This is the generic function! </code></pre> <p>I was hoping for:</p> <pre><code>This is the specialized function: - Type1 This is the generic function! We overloaded the specialized template function for type 1! This is the generic function! </code></pre> <p>So this still doesn't work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload