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 > template function question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
template function question

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 11h11   #1
Annonymous Coward
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut template function question

I have a "genertic dictionary type" object (MyDictionary) that provides
an accessor method that has the following general signature:


ReturnType getItem(TypeA row_specifier, TypeB col_specifier);

Where:

TypeA: {integer, char*}
TypeB: {integer, char*}
ReturnType: {integer, char*, double, bool, MyDictionary*}

What is the best way to implement this, other than writing seperate
functions for each possible combination of row_specifier, col_specifier
and return type?
  Réponse avec citation
Vieux 27/06/2008, 11h59   #2
rep_movsd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template function question

On Jun 27, 1:11pm, Annonymous Coward <m...@home.com> wrote:
> I have a "genertic dictionary type" object (MyDictionary) that provides
> an accessor method that has the following general signature:
>
> ReturnType getItem(TypeA row_specifier, TypeB col_specifier);
>
> Where:
>
> TypeA: {integer, char*}
> TypeB: {integer, char*}
> ReturnType: {integer, char*, double, bool, MyDictionary*}
>
> What is the best way to implement this, other than writing seperate
> functions for each possible combination of row_specifier, col_specifier
> and return type?


First of all, is the logic and code for each combination of types very
similar?
If not, you will end up writing a lot of specializations anyway, in
which case it may simply be easier to use separate overloaded
functions.


You cannot get overload and template type inference on return types -
One way to get this is to make the return argument an out patameter -
like this :

template<typename TypeA,typename TypeB,typename ReturnType>
getItem(TypeA row_specifier, TypeB col_specifier, ReturnType &ret)
{
// Whatever
}

// specialize as needed

that way you can call it easily like this and avoid specifying the
return type remplate parameter

double r1;
MyDictionary *r2;

getItem(1, 2, r1); // args int, int , return double
getItem("Foo", 7, r2); // args char*, int , return MyDictionary*


Vivek

  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 11h15.


É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,09106 seconds with 10 queries