In modern C++, which of the following are mechanisms of static (compile-time) polymorphism? Choose the option that best captures all techniques resolved before runtime.

Difficulty: Easy

Correct Answer: All of the above

Explanation:


Introduction:
Polymorphism in C++ broadly divides into static (compile-time) and dynamic (runtime). This question checks whether you can recognize the common features that provide static polymorphism—i.e., behavior decided by the compiler rather than a runtime dispatcher.


Given Data / Assumptions:

  • Language: C++ (standards-compliant, no compiler extensions assumed).
  • Focus: mechanisms whose resolution occurs during compilation.
  • Contrast with dynamic polymorphism via virtual functions.


Concept / Approach:
Static polymorphism means the call or instantiation choice is determined at compile time from types and overload sets. Three pillars in C++ are: function overloading (choosing among functions with the same name but different parameter lists), operator overloading (syntactic sugar that compiles down to function calls chosen at compile time), and templates (generic code that the compiler instantiates into concrete functions/classes based on supplied types). None of these require a vtable at runtime to choose an implementation.


Step-by-Step Solution:
1) Function overloading: the compiler selects the best match from the overload set using overload resolution rules.2) Operator overloading: operators map to functions (e.g., operator+); selection is determined at compile time similarly to ordinary overloads.3) Templates: the compiler generates code for specific types at instantiation time; calls are then resolved statically.4) Since all three are compile-time mechanisms, the comprehensive answer includes all.


Verification / Alternative check:
Inspect generated symbols (e.g., with objdump) to see template-instantiated functions and overloaded operators resolved to concrete functions without runtime dispatch tables.


Why Other Options Are Wrong:
Operator overloading: correct but incomplete alone.Function overloading: correct but incomplete alone.Templates: correct but incomplete alone.


Common Pitfalls:
Confusing templates with macros (templates are type-checked). Equating operator overloading with runtime dispatch (it is resolved at compile time unless combined with virtuals inside bodies).


Final Answer:
All of the above

More Questions from OOPS Concepts

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion