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 

question sur le préprocesseur...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Frédéric Gourul
Guest





PostPosted: Sat Jan 24, 2004 4:17 pm    Post subject: question sur le préprocesseur... Reply with quote



Bonjour,

Je n'aime pas trop utiliser les directives du préprocesseur, mais il y a des
cas où c'est quand même bien pratique... Je souhaiterai faire une définition
de classe template générique en fonction du nombre de paramètre template que
cette classe peut prendre.

J'illustre mon problème:

#define LIST_TP1 typename P1
#define LIST_TP2 LIST_TP1 , typename P2
#define LIST_TP3 LIST_TP2 , typename P3
#define LIST_TP(x) LIST_TP##x

#define NBARGS 3
#include clgen.h

-- fichier clgen.h --
si je fais: #define TEMPLATE_LIST LIST_TP(3) // ok
mais si: #define TEMPLATE_LIST LIST_TP(NBARGS) // marche pas :(

template <TEMPLATE_LIST>
class {....};
--------------------

Si je met directement la constante "3" à la macro LIST_TP(x) tout va bien
elle s'étend en LIST_TP3 ce qui est exactement ce que je veux. Mais si je
lui passe NBARGS, elle s'étend en LIST_TPNBARGS :(

Une idée ou une astuce pour faire ce que je veux hormis une batterie de test
du genre:
#if (NBARGS = ...)
#define TEMPLATE_LIST LIST_TP(...)
#elif ..
....
#endif

Merci de votre aide.


Back to top
Randolf Carter
Guest





PostPosted: Sun Jan 25, 2004 9:05 am    Post subject: Re: question sur le préprocesseur... Reply with quote



Quote:
J'illustre mon problème:

#define LIST_TP1 typename P1
#define LIST_TP2 LIST_TP1 , typename P2
#define LIST_TP3 LIST_TP2 , typename P3
#define LIST_TP(x) LIST_TP##x

#define NBARGS 3
#include clgen.h

-- fichier clgen.h --
si je fais: #define TEMPLATE_LIST LIST_TP(3) // ok
mais si: #define TEMPLATE_LIST LIST_TP(NBARGS) // marche pas
Sad


Essai comme ceci :
#define _LIST_TP(x) LIST_TP##x

#define LIST_TP(x) _LIST_TP(x)
et
#define TEMPLATE_LIST LIST_TP(NBARGS)
devrait fonctionner.

Cela provient du fait que l'opérateur ## concatène très exactement le
symbole passé en paramêtre. Ta solution donnait donc "LIST_TPNBARGS".
En passant par une macro intermédiaire, LIST_TP passera bien le
résultat de l'évaluation de NBARGS à _LIST_TP et te donnera le bon
"LIST_TP3".

--
- Randolf



Back to top
Jean-Marc Bourguet
Guest





PostPosted: Sun Jan 25, 2004 9:14 am    Post subject: Re: question sur le préprocesseur... Reply with quote



"Frédéric Gourul" <fgourul.nospam (AT) 9online (DOT) fr> writes:

Quote:
#define LIST_TP1 typename P1
#define LIST_TP2 LIST_TP1 , typename P2
#define LIST_TP3 LIST_TP2 , typename P3
#define LIST_TP(x) LIST_TP##x

Un classique du préprocesseur.

#define CONCAT(x,y) x ## y
#define LIST_TP(x) CONCAT(LIST_TP,x)

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
Frédéric Gourul
Guest





PostPosted: Sun Jan 25, 2004 1:53 pm    Post subject: Re: question sur le préprocesseur... Reply with quote


"Jean-Marc Bourguet" <jm (AT) bourguet (DOT) org> a écrit dans le message de
news:2d10vb.pq.ln (AT) news (DOT) bourguet.org...
Quote:
"Frédéric Gourul" <fgourul.nospam (AT) 9online (DOT) fr> writes:

#define LIST_TP1 typename P1
#define LIST_TP2 LIST_TP1 , typename P2
#define LIST_TP3 LIST_TP2 , typename P3
#define LIST_TP(x) LIST_TP##x

Un classique du préprocesseur.

#define CONCAT(x,y) x ## y
#define LIST_TP(x) CONCAT(LIST_TP,x)

Merci, c'est exactement ce que je voulais :)



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.