Virtual Function Members
A virtual function member cannot be a template member function.
Can a Member Be Both Virtual and a Template?
Two features of class design are useful on their own. 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. A natural question follows: can both features be combined on the same member function? This article answers that question and explains how to check it for any member you meet.
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, one member cannot have both properties at the same time.
The source states this as a rule: a virtual function member cannot be a template member function. The limitation concerns one member having both properties together.
The Two Properties to Check
A virtual function member is a member function that is marked as virtual. A template member function is a member function that is itself parameterized as a template. Each of these is a property that a single member function may or may not have.
The rule has two parts, and both must be present for the limitation to apply. Neither property is forbidden by this rule on its own. A member can be virtual without being a template, and a member can be a template without being virtual. The limitation applies only to the case where the two properties describe the same member.
- 1Look at the member function you want to classify.
- 2Ask the first question: is this member marked as virtual?
- 3Ask the second question: is this member itself a template member function?
- 4If the answer to both questions is yes, the member falls under the limitation. If either answer is no, this rule does not forbid the member.
Stating the Limitation as a Fixed Rule
The limitation is about the relationship between the two properties, not about either property alone. A virtual function member on its own is only the first property, and a template member function on its own is only the second. The rule forbids one member from having both at once. The diagram below shows the two properties and the combination that is ruled out.
The source states this limitation as a rule without giving a further explanation of its cause. For learning purposes, treat it as a fixed constraint of class design. When you design a class, you cannot make a member that is both virtual and a template member function.
Classifying Members Under the Rule
Classification follows the same procedure for every member. Keep the two questions separate and apply them to one member at a time. The table below uses generated descriptions of four members to show how the answers combine.
| Member description | Is it virtual? | Is it a template member function? | Classification |
|---|---|---|---|
| Virtual member, not a template | Yes | No | Allowed |
| Template member, not virtual | No | Yes | Allowed |
| Ordinary member, neither virtual nor a template | No | No | Allowed |
| Virtual template member | Yes | Yes | Not allowed |
Generated example: classifying members by applying the two questions
Classifying a virtual template member
A class has a member function that is marked as virtual and that is itself parameterized as a template. Is this member allowed under the rule?
- 1Ask the first question
The member is marked as virtual, so the first property is present.
- 2Ask the second question
The member is itself parameterized as a template, so the second property is present.
- 3Apply the rule
Both properties describe the same member, so the limitation applies.
Not allowed. This member is a virtual template member function, which the rule forbids.
Why the Limitation Shapes Class Design
Templates and virtual members are both ways of making a member flexible, but the rule means they cannot be stacked on one member. When you design a class and need a member to be a template, that member cannot also be virtual. When you need a member to be virtual, that member cannot also be a template. The choice is made per member, and it constrains the shape of the class.
Because the rule is a fixed constraint, a designer who wants both kinds of flexibility should plan for them on different members rather than expecting one member to provide both. Checking each property separately, as the procedure above does, is easier to apply during design than trying to remember a single sentence.
Mistakes to Avoid When Applying the Rule
Common mistake Treating each property as forbidden on its own
What it looks likeConcluding that a virtual member is not allowed because it is virtual.
Why it failsThe rule concerns only the combination of the two properties on one member. Neither property is forbidden by this rule alone.
Do this insteadAsk both questions about the same member. Only a member that is both virtual and a template member function falls under the limitation.
Common mistake Checking the two properties on different members
What it looks likeSeeing a virtual member in one part of a class and a template member elsewhere, then deciding the rule is broken.
Why it failsThe limitation is about one member having both properties at the same time. Two separate members do not violate it.
Do this insteadApply both questions to a single member function at a time.
Common mistake Memorizing the rule as a sentence without checking the two properties
Practice Classifying Members
A class contains three member functions. Member A is marked as virtual and is not a template. Member B is a template member function and is not marked as virtual. Member C is marked as virtual and is itself a template member function. Classify each member as allowed or not allowed under the rule, and state which of the two questions decided each answer.
Hints
- Ask the first question and the second question separately for each member.
- A member is not allowed only when both answers are yes for that same member.
Key Takeaways
- A virtual function member cannot be a template member function. This is the rule the article teaches.
- The limitation concerns one member having both properties at once. Each property alone is not forbidden by this rule.
- To classify a member, ask whether it is virtual and whether it is a template member function, then apply the rule to that same member.
- The source gives the rule without a reason, so treat it as a fixed constraint when designing a class.