C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template & Spécialisation

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Stephane Wirtel
Guest





PostPosted: Mon Apr 18, 2005 3:03 pm    Post subject: Template & Spécialisation Reply with quote



Bonjour à tous,

Voilà, je me pose le problème suivant :

Source :
--------

#include <iostream>
#include <string>

template <class T> void Print (const T& pValue) {
std::cout << "Print Generic : " << pValue << std::endl;
}

template <> void Print<int> (const int& pValue) {
std::cout << "Print : Int : " << pValue << std::endl;
}

template <> void Print<std::string> (const std::string& pValue) {
std::cout << "Print : String : " << pValue << std::endl;
}

int main (int argc, char **argv)
{
Print ("Implicit");
Print Print (10);
Print (10.20);
return 0;
}

Résultat :
----------

Print Generic : Implicit
Print : String : Explicit
Print : Int : 10
Print Generic : 10.2


Est-ce normal que pour 'Print ("Implicit")' j'ai le résultat 'Print Generic : Implicit' ?

N'aurais-je pas dû avoir 'Print : String : Implicit' à la place ?

compilateur : g++ 3.4.3

Merci
Back to top
Stephane Wirtel
Guest





PostPosted: Mon Apr 18, 2005 3:32 pm    Post subject: Re: Template & Spécialisation Reply with quote



Ahmed MOHAMED ALI a écrit :
Quote:
Bonjour,
Print<char*> ("Implicit") correspond mieux à l'appel que Print (std::string("Implicit"))
voila pourquoi.
Je m'en doutais un petit peu, mais comment faire pour qu'il le prenne comme étant un std::string ?


Back to top
Stephane Wirtel
Guest





PostPosted: Mon Apr 18, 2005 3:38 pm    Post subject: Re: Template & Spécialisation Reply with quote



En fait, j'essaie de créer une fonction générique qui retourne toujours un std::string, afin de le mettre dans une map.
Back to top
Ahmed MOHAMED ALI
Guest





PostPosted: Mon Apr 18, 2005 3:44 pm    Post subject: Re: Template & Spécialisation Reply with quote

Bonjour,
Print<char*> ("Implicit") correspond mieux à l'appel que Print<std::string>
(std::string("Implicit"))
voila pourquoi.

Ahmed


"Stephane Wirtel" <stephane.wirtel (AT) descasoft (DOT) com> wrote

Quote:
Bonjour à tous,

Voilà, je me pose le problème suivant :

Source :
--------

#include <iostream
#include
template std::cout << "Print Generic : " << pValue << std::endl;
}

template <> void Print<int> (const int& pValue) {
std::cout << "Print : Int : " << pValue << std::endl;
}

template <> void Print<std::string> (const std::string& pValue) {
std::cout << "Print : String : " << pValue << std::endl;
}

int main (int argc, char **argv)
{
Print ("Implicit");
Print Print (10);
Print (10.20);
return 0;
}

Résultat :
----------

Print Generic : Implicit
Print : String : Explicit
Print : Int : 10
Print Generic : 10.2


Est-ce normal que pour 'Print ("Implicit")' j'ai le résultat 'Print
Generic : Implicit' ?

N'aurais-je pas dû avoir 'Print : String : Implicit' à la place ?

compilateur : g++ 3.4.3

Merci



Back to top
Jean-Marc Bourguet
Guest





PostPosted: Mon Apr 18, 2005 3:51 pm    Post subject: Re: Template & Spécialisation Reply with quote

Stephane Wirtel <stephane.wirtel (AT) descasoft (DOT) com> writes:

Quote:
template <class T> void Print (const T& pValue) {
template <> void Print<int> (const int& pValue) {
template <> void Print<std::string> (const std::string& pValue) {

int main (int argc, char **argv)
{
Print ("Implicit");
Print<std::string> ("Explicit");
Print (10);
Print (10.20);
return 0;
}

Résultat :
----------

Print Generic : Implicit
Print : String : Explicit
Print : Int : 10
Print Generic : 10.2


Est-ce normal que pour 'Print ("Implicit")' j'ai le
résultat 'Print Generic : Implicit' ?

Oui. Le type de "Implicit" n'est pas std::string mais
char const[9], donc tu n'appelles pas la version spécialisée
pour std::string. Peut-être plus surprenant,

template <> void Print<char const*> (char const* const& pValue) {
std::cout << "Print : char const* : " << pValue << std::endl;
}

ne serait pas non plus utilisée. Il faut:

typedef char const array[9];
template <> void Print<array>(array const& pValue) {
std::cout << "Print : char const [9] : " << pValue << std::endl;
}

Mais gcc (3.3.4 en tout cas) a un bug et il faut enlever le
const au typedef.

A+

--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ
C++ FAQ Lite en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org

Back to top
Christophe de Vienne
Guest





PostPosted: Tue Apr 19, 2005 7:45 am    Post subject: Re: Template & Spécialisation Reply with quote

Stephane Wirtel wrote:

Quote:
Ahmed MOHAMED ALI a écrit :
Bonjour,
Print<char*> ("Implicit") correspond mieux à l'appel que
Print<std::string> (std::string("Implicit"))
voila pourquoi.
Je m'en doutais un petit peu, mais comment faire pour qu'il le prenne
comme étant un std::string ?

Surcharger Print<const char *>() ?

A+

Christophe


Back to top
Jean-Marc Bourguet
Guest





PostPosted: Tue Apr 19, 2005 8:25 am    Post subject: Re: Template & Spécialisation Reply with quote

Christophe de Vienne <cdevienne (AT) alphacent (DOT) com> writes:

Quote:
Stephane Wirtel wrote:

Ahmed MOHAMED ALI a écrit :
Bonjour,
Print<char*> ("Implicit") correspond mieux à l'appel que
Print<std::string> (std::string("Implicit"))
voila pourquoi.
Je m'en doutais un petit peu, mais comment faire pour qu'il le prenne
comme étant un std::string ?

Surcharger Print<const char *>() ?

Ça ne sert à rien dans ce cas. Voir mon message.

A+

--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ
C++ FAQ Lite en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org

Back to top
Alexandre
Guest





PostPosted: Sat Apr 23, 2005 7:07 pm    Post subject: Re: Template & Spécialisation Reply with quote

Quote:
Print ("Implicit");

"Implicit" n'est pas du type std::string. Donc il te faudra appeler
Print(std::string("Implicit"))
ou redéfinir ton template pour les char *.




Back to top
Jean-Marc Bourguet
Guest





PostPosted: Sat Apr 23, 2005 7:15 pm    Post subject: Re: Template & Spécialisation Reply with quote

"Alexandre" <alex.g (AT) netcourrier (DOT) com> writes:

Quote:
Print ("Implicit");

"Implicit" n'est pas du type std::string. Donc il te faudra appeler
Print(std::string("Implicit"))
ou redéfinir ton template pour les char *.

Non. Voir [email]877jj0qe0z.fsf (AT) news (DOT) bourguet.org[/email]

A+

--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ
C++ FAQ Lite en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org

Back to top
Fabien LE LEZ
Guest





PostPosted: Sat Apr 23, 2005 10:47 pm    Post subject: Re: Template & Spécialisation Reply with quote

On 18 Apr 2005 17:51:24 +0200, Jean-Marc Bourguet <jm (AT) bourguet (DOT) org>:

Quote:
Est-ce normal que pour 'Print ("Implicit")' j'ai le
résultat 'Print Generic : Implicit' ?

[...] Peut-être plus surprenant,

template <> void Print<char const*> (char const* const& pValue) {
std::cout << "Print : char const* : " << pValue << std::endl;
}

ne serait pas non plus utilisée.

Et quid de

void Print (char const* const& pValue)

?


--
;-)

Back to top
Jean-Marc Bourguet
Guest





PostPosted: Sun Apr 24, 2005 7:51 am    Post subject: Re: Template & Spécialisation Reply with quote

Fabien LE LEZ <gramster (AT) gramster (DOT) com> writes:

Quote:
On 18 Apr 2005 17:51:24 +0200, Jean-Marc Bourguet <jm (AT) bourguet (DOT) org>:

Est-ce normal que pour 'Print ("Implicit")' j'ai le
résultat 'Print Generic : Implicit' ?

[...] Peut-être plus surprenant,

template <> void Print<char const*> (char const* const& pValue) {
std::cout << "Print : char const* : " << pValue << std::endl;
}

ne serait pas non plus utilisée.

Et quid de

void Print (char const* const& pValue)

Ça ne change rien: la spécialisation sur char const[9] est
toujours meilleure est donc sera générée si elle n'est pas
explicite. Pour que la conversion implicite
tableau->pointeur intervienne, il faut qu'il n'y ait pas de
template en vue.

A+

--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ C++ FAQ Lite
en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.