Which of the following is not correct (in C++) ?

2017

Which of the following is not correct (in C++) ?

  1. A.

    Class templates and function templates are instantiated in the same way.

  2. B.

    Class templates differ from function templates in the way they are initiated.

  3. C.

    Class template is initiated by defining an object using the template argument.

  4. D.

    Class templates are generally used for storage classes.

Attempted by 106 students.

Show answer & explanation

Correct answer: A

Answer: The statement "Class templates and function templates are instantiated in the same way." is not correct.

Why this is incorrect:

  • Function templates: Instantiated at the point of a function call; template arguments are commonly deduced from the function arguments (template argument deduction).

  • Class templates: Instantiated when a concrete type is required (for example when you define an object, use a member, or explicitly instantiate). Template arguments are usually specified explicitly; class template argument deduction (CTAD) is available in limited cases since C++17.

  • Key distinction: The trigger and mechanism for instantiation differ: function templates are tied to calls and deduction, whereas class templates are tied to type use and usually explicit arguments.

Comments on the other statements:

  • The statement that class templates differ from function templates in how they are initiated is accurate given the differences above.

  • The statement that a class template is initiated by defining an object using template arguments is a common and correct description of how class template instantiation is triggered (with CTAD being a modern exception).

  • The statement that class templates are generally used for storage classes is reasonable if interpreted to mean container or data-holding types, since many standard containers are class templates (for example std::vector<T> and std::map<Key, T>).

Summary: The incorrect statement is the claim that class templates and function templates are instantiated in the same way. They have different instantiation triggers and different common mechanisms for obtaining template arguments.

Explore the full course: Mppsc Assistant Professor