Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:38:00+00:00 2026-06-10T13:38:00+00:00

A template class is a multitude of possible classes, so I was asking me:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:38:01+00:00Added an answer on June 10, 2026 at 1:38 pm

    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:

    template <typename T> class My_Class { };
    template <typename T> class My_Class { };
    
    // Error: redefinition of ‘class Foo<T>’
    

    Repeat inclusion is very easy to occur. For example, consider a case where you include A and B, and A already includes B for some undocumented reason. You may not be entitled to omit B from your explicit includes, but neither should A be 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:

    template <typename T> class Foo;
    template <typename T> class Foo;
    template <typename T> class Foo;
    template <typename T> class Foo;
    template <typename T> class Foo { };
    int main() {}
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class that defines a subtype. I'm trying to define the
Given a C++ template class (or function) definition: template<typename T> struct Foo { T
I have a template class in C++ (somewhat simplified): template<typename T> struct C {
Consider a template class like: template<typename ReturnType, ReturnType Fn()> class Proxy { void run()
If I have a template class specification like so, template <typename T> class MyClass
Suppose I have a template class MyClass. Is it possible to store the type
I have a template class with private constructor and destructor: template <typename T> class
I have the following template class template <typename threadFuncParamT > class ThreadPool { //
I have a template class which implements function: template<typename T> class Matrix { ...
I have a template class Test, and I am trying to define the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.