Template Member Functions
The limitation concerns the combination of the two properties on one member. Neither property is forbidden by this rule on its own.
Why Two Good Features Raise a Question
Templates let you write one piece of code that works for many types. Member functions can be made virtual so that a call is resolved at run time. Both features are useful on their own, and a natural question follows: can both be combined on the same member function? The answer is part of the rules for class design, and it is worth working out before you read the rule itself.
What do you think happens?
Before reading on, make your own guess: can a single member function be both virtual and a template member function?
Reveal answer
No, a virtual member function cannot also be a template member function.
The rule is stated directly in the source. Each feature is allowed on its own, but the two cannot be combined on the same member.
The Rule and Its Two Parts
A virtual function member cannot be a template member function. This rule has two parts, and both must be present for the limitation to apply. The first part is a virtual function member, meaning a member function marked as virtual. The second part is a template member function, meaning a member function that is itself parameterized as a template.
The limitation concerns the combination of the two properties on one member. Neither property is forbidden by this rule on its own.
It helps to keep the two properties separate in your head. Each one describes a member function, and the limitation says the two descriptions cannot both apply to the same member at the same time. A virtual member on its own is the first property. A template member on its own is the second property. The rule forbids only the case where one member has both.
Checking a Member Step by Step
The same procedure works for any member function you meet. Ask two questions: is the member virtual, and is the member a template member function? If the answer to both is yes, the member falls under the limitation and is not allowed. If either answer is no, the rule does not forbid that member. Checking each property separately is easier to apply than trying to remember a single sentence.
Classifying Four Kinds of Members
The clearest way to see the rule is to sort a small set of member functions into allowed and not allowed. The example below is generated for teaching purposes and is not taken from the source. It shows one class for each of the four combinations of the two properties.
- area is virtual but not a template, so the first property alone does not trigger the rule
- log is a template but not virtual, so the second property alone does not trigger the rule
- save has both properties on one member, which is the combination the rule forbids
Only the member that is both virtual and a template falls under the limitation. The other three are not forbidden by this rule. Notice that the template member and the virtual member each remain legal when they appear alone.
Why the Limitation Matters in Design
The source states this limitation as a fixed rule and does not give the reason behind it. For learning purposes, treat it as a constraint on class design: when you design a class, you cannot make a member that is both virtual and a template member function. This matters because a designer who wants a run-time choice of behavior and a type-generic member at once will find that one member cannot provide both. The design must separate them.
When a design seems to need both features on one member, the usual response is to split the work. One member can stay virtual and call into a separate, non-virtual template member that does the type-generic work. This keeps each member within the rule. The split is a design choice you make from the constraint, not a rule the source spells out.
Applying the Check to a New Class
Checking a proposed member
A designer proposes a member function named process that is virtual and takes a parameter of any type through a template. Is this member allowed under the limitation?
- 1Identify the first property
The member is described as virtual, so the first property holds.
- 2Identify the second property
The member takes a parameter of any type through a template, so it is a template member function. The second property holds.
- 3Apply the rule to the same member
Both properties apply to the same member at the same time. The rule forbids exactly this combination.
- 4Decide what to change
Keep one property on each member. For example, keep process virtual with a fixed parameter type, and move the type-generic work into a separate non-virtual template member.
As proposed, process is not allowed, because it is both virtual and a template member function. Splitting it into a virtual member and a non-virtual template member makes each member allowed.
Mistakes Learners Often Make
Common mistake Thinking that virtual members cannot be used in any class that has templates
Why it failsThe rule applies only to one member having both properties. The class can have virtual members and template members at the same time.
Do this insteadCheck each member separately. Ask whether that one member is virtual and whether that same member is a template.
Common mistake Thinking that a template member is forbidden because it is generic
Why it failsA template member on its own is not covered by this rule. Only the combination with virtual is ruled out.
Do this insteadA non-virtual template member is allowed under this rule.
Common mistake Trying to memorize one sentence instead of applying two questions
Why it failsA single sentence is easy to misremember, for example by dropping the word virtual or the word template.
Do this insteadAsk the two questions in order: is it virtual, and is it a template member function? Both yes means the limitation applies.
Classify Before You Design
A class has three member functions: a virtual function named render with a fixed parameter list, a non-virtual template function named store, and a virtual template function named update. For each one, say whether it is allowed under the limitation, and name the two questions you used to decide.
Hints
- Ask whether each member is virtual, then whether it is a template member function.
- Only a member where both answers are yes falls under the limitation.
- Check update last, since it is the one that has both properties.
Key Takeaways
- A virtual function member cannot be a template member function. The limitation applies only when both properties hold for the same member.
- A virtual member on its own is allowed, and a template member on its own is allowed. Only the combination on one member is ruled out.
- To classify any member, ask two questions in order: is it virtual, and is it a template member function. Both yes means the member is not allowed.
- The source gives this rule without a reason, so treat it as a fixed constraint on class design. When a design needs both features, split them across separate members.