Reputation: 387
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
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
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