473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6177

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.