H
Hartmut Sbosny
Hello NG,
I have a question. I have a header file with a function template and a fully
specialized version of it, for instance
//======== File "templ.hpp" ========
#include <iostream>
// the general function template...
template <class T>
void print (T x) { std::cout << "x = " << x << std::endl; }
// the specialized function template...
template<>
void print<int>(int x) { std::cout << "int: x = " << x << std::endl; }
//========= end of file ===========
This header file is included in two source files:
//======== File "foo.cpp" ========
#include "templ.hpp"
//========= end of file ===========
//======== File "main.cpp" ========
#include "templ.hpp"
int main() {
print(4.2);
print("a C-string");
print(2);
}
//========= end of file ===========
The source files are compiled separately to object files.
When I link the object files, the linker gives the error
"multiple definition of `void print<int>(int)'".
It seems that the compiler interprets the sepecialized function template
as a common, non-inline function. Is this a language convention or a
compiler bug (gcc 3.3.4 (pre 3.3.5 20040809)) or something third one?
Thanks in advance
Hartmut
I have a question. I have a header file with a function template and a fully
specialized version of it, for instance
//======== File "templ.hpp" ========
#include <iostream>
// the general function template...
template <class T>
void print (T x) { std::cout << "x = " << x << std::endl; }
// the specialized function template...
template<>
void print<int>(int x) { std::cout << "int: x = " << x << std::endl; }
//========= end of file ===========
This header file is included in two source files:
//======== File "foo.cpp" ========
#include "templ.hpp"
//========= end of file ===========
//======== File "main.cpp" ========
#include "templ.hpp"
int main() {
print(4.2);
print("a C-string");
print(2);
}
//========= end of file ===========
The source files are compiled separately to object files.
When I link the object files, the linker gives the error
"multiple definition of `void print<int>(int)'".
It seems that the compiler interprets the sepecialized function template
as a common, non-inline function. Is this a language convention or a
compiler bug (gcc 3.3.4 (pre 3.3.5 20040809)) or something third one?
Thanks in advance
Hartmut