What will be printed out as a result of the following code execution?
#include <iostream>

struct A {
  virtual int f(int) {
      return 1;

struct B : A {
  template <typename T>
  int f(T) {
    return 2;

int main(int argc, char**argv) {
  A* pa = new B;

  std::cout << pa->f(1) << '\n';

  delete pa;
  return 0;
According to the standard, template member functions do not overload the virtual functions of the base class. Thus, the first function will be called. Not to be confused with virtual functions of template classes.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes