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
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.
Answer checked How we write and check questions
Discussion