Machine-translated by Google. Questions are authored in English.
Top

Discussion

In C++ templates, which statement is TRUE ?

  • A.Templates support runtime polymorphism
  • B.Template functions cannot be overloaded
  • C.Templates generate code during compilation
  • D.Templates must have only one type parameter

Answer: C

The true statement is that templates generate code during compilation.

A C++ template is a blueprint. When it is used with particular types, for example max<int> or vector<string>, the compiler instantiates it, generating a separate concrete function or class for those types at compile time. This is why templates give compile-time (static) polymorphism with no run-time cost.

Run-time polymorphism comes from virtual functions, not templates, so option A is false. Function templates can be overloaded, with each other and with ordinary functions. And templates may take several type parameters, as std::map<Key, Value> shows.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.