Re: Help with operator overloading definition and (possibly) link problems
Noob <nope@nono.com> wrote:
$ cat foo.cpp
#include <array>
#include "foo.h"
template<typename T, std::size_t SIZE>
std::array<T,SIZE> operator+(const std::array<T,SIZE>& a,
const std::array<T,SIZE>& b)
{
std::array<T,SIZE> result;
for(unsigned int i = 0; i < a.size(); ++i)
{
result[i] = a[i] + b[i];
}
return
result;
}
You need to instantiate it in *this* compilation unit with every
type it's being used with anywhere else.
Unfortunately since C++ does not support export templates (anymore),
the compiler won't do it for you. You'll have to do it manually.
If you want to do it like that, you need explicit instantiation
(google for "template explicit insantiation").
However, the easiest way is to simply do it like you did in your
first example, and put the implementation in the same header file
where the declaration is.
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---