472,360 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,360 software developers and data experts.

fully specialized function template and "multiple definition"

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
2 6105

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
5
by: Maett | last post by:
Hi. When I try to compile this piece of Code with VC.NET 2003 // code start #include <vector> template< class T > struct Marker { public:
6
by: Don Leverton | last post by:
Hi All, I've got a situation where I am developing an Access 97 app for a client, and am in the "beta testing" stage. I have split the app up, using the DB splitter, into front-end /back-end...
10
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
4
by: DR | last post by:
when i build someone's project, and right click a function "goto definition" is disabled. how to reconfigure a .net project to support "goto definition" when i right click on function calls?
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.