 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frédéric Gourul Guest
|
Posted: Sat Jan 24, 2004 4:17 pm Post subject: question sur le préprocesseur... |
|
|
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
|
Posted: Sun Jan 25, 2004 9:05 am Post subject: Re: question sur le préprocesseur... |
|
|
| 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
 |
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
|
|
| Back to top |
|
 |
Frédéric Gourul Guest
|
Posted: Sun Jan 25, 2004 1:53 pm Post subject: Re: question sur le préprocesseur... |
|
|
"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 |
|
 |
|
|
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
|
|