tisch
tisch

Reputation: 1118

Mixing function templates and ordinary functions

I have a couple of small helper functions declared in a header file. It looks like this:

//contents of foo.h
#ifndef FOO_H
#define FOO_H

void foo1(int a);
template <class mType>
void foo2( mType b);

#endif

//contents of foo.cpp
#include foo.h

void foo1(int a)
{
  ...
}

template <class mType>
void foo2( mType a)
{
  ...
}

Normally when having only function templates i would add an #include "foo.cpp" at the end of foo.h to make the implementation of the template functions visible to the compiler at compile time. However when mixing function templates and ordinary functions this approach doesn't seem to work. How would one resolve having template functions and ordinary functions in such a case?

Upvotes: 0

Views: 225

Answers (1)

Pubby
Pubby

Reputation: 53047

You should never include a cpp file.

Put the implementation of the template in a header file. If you want to keep it separate then make 2 header files.

//contents of foo.h

void foo1(int a);

template <class mType>
void foo2( mType a)
{
...
}

//contents of foo.cpp
#include foo.h

void foo1(int a)
{
...
}

(Alternatively, there is the export keyword, although no major compilers support it and it has been removed in C++11 In other words, don't use it)

Upvotes: 2

Related Questions