This little piece of code triggers the linker's anger when included on at least two translation units (cpp files) :
# ifndef MAXIMUM_HPP
# define MAXIMUM_HPP
template<typename T>
T maximum(const T & a, const T & b)
{
return a > b ? a : b ;
}
/* dumb specialization */
template<>
int maximum(const int & a, const int & b)
{
return a > b ? a : b ;
}
# endif // MAXIMUM_HPP
But compiles and links fine with one translation unit. If I remove the specialization, it works fine in all situations. Here is the linker message :
g++ -o test.exe Sources\test.o Sources\other_test.o
Sources\other_test.o:other_test.cpp:(.text+0x0): multiple definition of `int maximum<int>(int const&, int const&)'
Sources\test.o:test.cpp:(.text+0x14): first defined here
Aren't templates allowed to be instantiated multiple times ? How to explain this error and how to fix it ?
Thanks for any advice !