Practice mode C++

Why are C++ function and class template definitions normally placed in header files?

Correct answer D. Because the full definition must be visible wherever the template is instantiated

Explanation

A template is not code until it is instantiated with specific arguments, and the compiler needs the complete definition in each translation unit that uses it with new arguments. If the definition lives in a separate .cpp file, other files see only a declaration and you get undefined-reference linker errors, unless explicit instantiation is used for a known, fixed set of types.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.