A template class is a multitude of possible classes, so I was asking me: the preprocessor directives
#ifndef MY_CLASS
#define MY_CLASS
template<typename T>
class My_Class
{};
#endif
are necessaries?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The include guards prevent repeated inclusion of the same file. You need them to prevent a translation unit that would contain the following definitions, which are illegal:
Repeat inclusion is very easy to occur. For example, consider a case where you include
AandB, andAalready includesBfor some undocumented reason. You may not be entitled to omitBfrom your explicit includes, but neither shouldAbe required to not include it. Only by using include guards (or some equivalent mechanism) can you make such a file structure possible.In a nutshell: You can have repeated declarations, but only one definition within one translation unit.
Counter example: The following is legal: