1

I've read that the body of a template function must be included in the file which actually uses it (i.e., the decleration isn't enough).

Suppose that I define a template function in a header file:

//someheader.h
#ifndef SOME_H
#define SOME_H
template<class T>
void MyTemplateFunc(T *In, ...)
{

//definitions here   

}
#endif

I can actually use it in two different cpp files:

//file1.cpp
#include "someheader.h"
void f1()
{
   //some code
   MyTemplateFunc<int>(Some_int_Pointer);//use template here

}

and

//file2.cpp
#include "someheader.h"
void f2()
{
   //some code
   MyTemplateFunc<float>(Some_float_Pointer);//use template here

}

Now, I am not complaining (just trying to understand) but how come I am not getting compiling/linking error in this case?. Since the double inclusion guard will cause "someheader.h" to be included only in one of the cpp files which in turn will cause the other cpp file to complain that he can't "see" the template definition.

What am I missing here?

Thanks

Benny

2
  • Is there actual code that you will try compile? You have missed ";" symbol at "MyTemplateFunc" call. If you miss, this will lead to syntax error. Commented Nov 13, 2013 at 16:36
  • "Since the double inclusion guard will cause "someheader.h" to be included only in one of the cpp files" double inclusion guard? The preprocessor is run on each source file individually. Macros declared in (header files included in) a source file A.cpp are only declared in A.cpp and not (automatically) in any other source file. Commented Nov 13, 2013 at 16:36

2 Answers 2

1

"Since the double inclusion guard will cause "someheader.h" to be included only in one of the cpp files"

It's false. That guard avoids multiple declarations in a translation unit. So, you have included it in each translation unit once and everything is OK.

In addition you should inline that function in the header file to obey one definition rule.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the explanation that I've looked for!!.
1

It seems to me that, your file1.cpp and file2.cpp are two different files, independent of each other.

You would had got the error msg, and your code had failed to run, if :

if you had not used ifndef and you had included file1.h in file2.h , then in file2.h you had declaration of someheader.h" twice because it is already declared in file1.h,

//someheader.h

template<class T>
void MyTemplateFunc(T *In, ...)
{

  //definitions here   

}

file1 header file

//file1.h
#include "someheader.h"

void f1();

and file2 header file

//file2.h

#include "file1.h" //! note, it already has declared someheader.h
#include "someheader.h" //! you are declaring it again
void f2();

In such case to solve the issues like this , you should include ifndef or #pragma once, which would cause the current source file to be included only once in a single compilation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.