473,854 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6201

"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
8616
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 keep getting errors thrown that I can't verify because the form processes onto itself too quickly for me to check the Javascript errors) because the select multiple form element name has to be in the form of "var" because PHP will then recognize...
5
6355
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
2542
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 for the usual reason ... so I can mess with form/report revisions. Since splitting, I've had issues with re-linking the tables ... as it relates to the completetely different "My Documents" data paths on several different Win98 / Win XP computers.
10
20903
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 setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
7
16584
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 called "Point" which would have two values "x" and "y". Now, let's say if it were the Java version, I would want two constructors: one that accept two numbers, the other accepts a string:
9
4107
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 puts() to print to screen ????
4
1476
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
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9751
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10678
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10751
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7914
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4153
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.