PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Using templates
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using templates

Réponse
 
LinkBack Outils de la discussion
Vieux 15/01/2008, 11h43   #1
Sunil Varma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using templates

Hi,

I'm trying to write template functions and use them.

Is it possible to declare a template function in a .h file and define
it in a .cpp file.

Ex:

//temp_func.h
template <class T>void swap(T &left,T &right);

//temp_func.cpp
template <class T>void swap(T &left,T &right)
{
T temp = left;
left = right;
right = temp;
}

//main.cpp
#include <iostream>
#include "temp_func.h"

using namespace std;
main()
{
int a = 10, b = 20;
cout<<"Before swapping: a = "<<a<<", b = "<<b<<endl;
swap(a,b);
cout<<"After swapping: a = "<<a<<", b = "<<b<<endl;
}

When I built the above code, I got the following error:

undefined reference to 'void swap<int>(int&,int&)'

I know that the instantiation of the swap() function is not available
in temp_func.o.

But, how can one make the above code build successfully.

The above code builds properly if I define the swap() function in
the .h file itself.

How do the STL algorithms work?

Are those functions defined in the headers itself.

Thanks in advance.

Regards
Sunil
  Réponse avec citation
Vieux 15/01/2008, 11h56   #2
tragomaskhalos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using templates

On 15 Jan, 11:43, Sunil Varma <sunil....@gmail.com> wrote:
> Hi,
>
> I'm trying to write template functions and use them.
>

[snip]

See http://www.parashift.com/c++-faq-lit...html#faq-35.12

hth

  Réponse avec citation
Vieux 16/01/2008, 08h18   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using templates

On Jan 15, 12:43 pm, Sunil Varma <sunil....@gmail.com> wrote:

> I'm trying to write template functions and use them.


> Is it possible to declare a template function in a .h file and
> define it in a .cpp file.


Yes, but...

> Ex:


> //temp_func.h
> template <class T>void swap(T &left,T &right);


You need to declare it extern here. Which is a problem, since a
lot of compilers aren't very up-to-date, and don't support
extern.

> //temp_func.cpp
> template <class T>void swap(T &left,T &right)
> {
> T temp = left;
> left = right;
> right = temp;
> }


This is, obviously, the only reasonable organization. Given
that compilers don't support extern, howver, what you have to do
is add an ``#include "temp_func.cpp"'' to the end of your .h
file (and of course, ensure that the .cpp is exported along with
the .h file, when you provide a library).

A common convention is to use still a different naming
convention for these files. G++, for example, terminates them
with .tcc (as opposed to .cc and .hh). Typically, they are
designed to work only when included at the end of the .hh, and
shouldn't be included anywhere else.

Another possibility would be to use export when available, using
conditional compilation. Basically, your header looks something
like:

#ifndef TEMP_FUNC_CPP_andSomeRandomJunk

export template< typename T > void swap( T& left, T& right ) ;

#ifdef export
#include "temp_func.cpp"
#endif
#endif

If the compiler doesn't support export, you invoke it with
something like -Dexport= or /Dexport=, whatever is necessary to
define "export" as an empty string.

[...]
> How do the STL algorithms work?


> Are those functions defined in the headers itself.


It depends on the implementation---g++ does as I described, it
puts the implementations in a .tcc file, which is included from
the .hh.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h24.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,40155 seconds with 11 queries