fully specialized function template and "multiple definition"

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
 
S

Sharad Kala

Hartmut Sbosny said:
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?

Not a compiler bug.
Full specialization does not declare a template, and therefore only one
*definition* of a non-inline full function template specialization should
appear in a program.

Sharad
 
H

Hartmut Sbosny

Sharad said:
Hartmut Sbosny said:
[...]
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?

Not a compiler bug.
Full specialization does not declare a template, and therefore only one
*definition* of a non-inline full function template specialization should
appear in a program.

Thank you.

Hartmut
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top