dheeraj Vadlani
dheeraj Vadlani

Reputation: 387

Redefinition error with header files - cpp

I have a template functions defined in a header file under a namespace. When I include this header in two source file in the same project. I don't get redefinition error.

/* template.h */
namespace x 
{
   template<typename T>
   function(t)
   {
       /* implementation */
   }
}

/*test.cpp*/
#include "template.h"

/* test2.cpp */
#inlcude "template.h"

In the above case I don't get any redefinition error. .Why I am not receiving any error?

Upvotes: 0

Views: 928

Answers (2)

Awais Rafique
Awais Rafique

Reputation: 476

Define the Header fine in the inner header file, when you include the inner header file to outer file all the headers will be included.

#ifndef FILE_H
#define FILE_H

/* ... Declarations etc here ... */

#endif

Upvotes: 0

Quentin
Quentin

Reputation: 63124

Because implicit template instantiations behave as if they were implicitly inline: all of them are consolidated into a single one at link-time.

Upvotes: 2

Related Questions