By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

fully specialized function template and "multiple definition"

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Hartmut Sbosny" <ha************@gmx.de> wrote in message > 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?


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
Jul 23 '05 #2

P: n/a
Sharad Kala schrieb:
"Hartmut Sbosny" <ha************@gmx.de> wrote in message
[...]
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.