BOOST_PP_ITERATE
以åã®è¨äºã§BOOST_PP_REPEATã«ã¤ãã¦æ¸ãã¾ããã BOOST_PP_REPEATã¯ä¼¼ããããªè¨è¿°ãç¹°ãè¿ãéã«å©ç¨ã§ãã¾ããä¾ãã°ã以ä¸ã®æ§ã§ã(ãã®ã³ã¼ãèªä½ã«æå³ã¯ããã¾ãããâ¦)
#include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/repeat.hpp> #define VALUE(z, n, d) const int BOOST_PP_CAT(VALUE_, n) = n; BOOST_PP_REPEAT(4, VALUE, _)
å±éå¾
const int VALUE_0 = 0; const int VALUE_1 = 1; const int VALUE_2 = 2; const int VALUE_3 = 3;
æ°´å¹³å復ã¨åç´å復
BOOST_PP_REPEATã¯1ã¤ã®è¡ã«å復ããã³ã¼ããçæãã¾ããããªããã»ããµã¨åãè¡ã«å復å±éãããã¨ãæ°´å¹³å復ã¨ããã¾ããä¸è¨ã®ãããªä¾ã§ã¯ç¹ã«åé¡ã¯ããã¾ããããã¯ã©ã¹å®ç¾©ãªã©ãæ°´å¹³å復ã§å®ç¾©ããã¨å®éã«çæãããã³ã¼ãã¯1è¡ãªã®ã§è¦ã¥ãããæ£ããæ¸ãã¦ããã確ããããæããã¤ã³ããªã»ã³ã¹ãªã©ã§å±éã³ã¼ãè¦ãæãããããã°ãããæã«åãä½ç½®ã«é£ãã ããã¦è¦ã¥ããä¸ä¾¿ã§ãããããã£ãå ´åãè¤æ°è¡ã«å±éã§ããåç´å復ãæå¹ã«ãªãã¾ããããã¦ãåç´å復ããæ¹æ³ã®1ã¤ã«BOOST_PP_ITERATEãããã¾ãã
ãµã³ãã« file.h
#ifndef BOOST_PP_IS_ITERATING # ifndef FILE_H # define FILE_H # include <boost/preprocessor/iteration/iterate.hpp> # include <boost/preprocessor/cat.hpp> # define BOOST_PP_ITERATION_LIMITS (0, 4) # define BOOST_PP_FILENAME_1 "file.h" # include BOOST_PP_ITERATE() # endif #else # define n BOOST_PP_ITERATION() const int BOOST_PP_CAT(VALUE_, n) = n; # undef n #endif
å±éå¾
const int VALUE_0 = 0; const int VALUE_1 = 1; const int VALUE_2 = 2; const int VALUE_3 = 3; const int VALUE_4 = 4;
ã¨è¤æ°è¡ã«å復ããã¦ãã¾ãã
ä»åã¯èªå·±å復ã¨ããæ¹æ³ã使ã£ã¦ãã¾ããèªåèªèº«ã®ãã¡ã¤ã«ãç¹°ãè¿ãincludeãããã¨ã§åç´å復ãå®ç¾ãã¦ãã¾ããBOOST_PP_ITERATEã«ãã£ã¦ãã¡ã¤ã«ãincludeãããå ´åãå復ä¸ã¨ãããã¨ã表ãããã«BOOST_IS_ITERATINGãå®ç¾©ããã¦ãã¾ããå復ä¸ã§ãªãå ´åã¯å®ç¾©ããã¦ãã¾ããã
#ifndef BOOST_PP_IS_ITERATING //å復ä¸ã§ãªã(1åç®ã®include) //ããã§å復åã®åæåãè¨å®ãè¡ã #else //åå¾©ä¸ //å復ä¸ã®å¦çãæ¸ã #endif
#ifndef BOOST_PP_IS_ITERATING
ç¯ãè¦ã¦ã¿ã¾ãããã
# ifndef FILE_H # define FILE_H ... # endif
#ifndef FILE_H
ãã#endif
ã¾ã§ã¯æ®éã®å¤éã¤ã³ã¯ã«ã¼ãã¬ã¼ãã§ããããã«åç´å復ãããã³ã¼ãã®åæç¶æ
ãæ¸ããããã¾ããåç´å復ã§ã¯ã©ã¹ãã³ãã¬ã¼ãã®ç¹æ®åãæ¸ãããå ´åã¯ããã«ãã©ã¤ããªã¼ãã³ãã¬ã¼ãã¯ã©ã¹ãæ¸ãããããªã©ã§ãã
#ifndef BOOST_PP_IS_ITERATING # ifndef FILE_H # define FILE_H # include <boost/preprocessor/iteration/iterate.hpp> # include <boost/preprocessor/cat.hpp> # define BOOST_PP_ITERATION_LIMITS (0, 4) # define BOOST_PP_FILENAME_1 "file.h" # include BOOST_PP_ITERATE() # endif
BOOST_PP_ITERATEã¯boost/preprocessor/iteration/iterate.hppã«ããã¾ããBOOST_PP_ITERATEã使ãããå®ç¾©ããªããã°ãªããªããã¯ãã2ã¤ããã¾ãã
1ã¤ã¯BOOST_PP_ITERATION_LIMITSã¨ããã¿ãã«ã§ããããã¯ãä½åç¹°ãè¿ãããã®ãã®æ°ã(<æåã®æ°>, <çµããã®æ°>)ã¨å®ç¾©ãã¾ãã2ã¤ãã¯BOOST_PP_FILENAME_1ã§ããç¹°ãè¿ãincludeãããã¡ã¤ã«ãæå®ãã¾ããä»åã¯èªåèªèº«ãªã®ã§"file.h"ã¨æ¸ãã¾ããããã¦æå¾ã«#include BOOST_PP_ITERATE()
ã¨å¼ã³åºããã¨ã§å復éå§ã§ãã
次ã«å復ä¸ã«å±éããã#elseç¯ãè¦ã¦ã¿ã¾ãããã
# define n BOOST_PP_ITERATION() const int BOOST_PP_CAT(VALUE_, n) = n; # undef n
ç¾å¨ç¹°ãè¿ãã®ä½åç®ãã¯BOOST_PP_ITERATION()
ã§åå¾åºæ¥ã¾ããä»åã®å ´åã¯0ãã4ã®ãã¼ã¯ã³ã«ãªãã¾ãããã®ã¾ã¾ä½¿ããã¨ãã§ãã¾ãããè¦ã¥ããã«æ¸ãã®é¢åãªã®ã§n
ã¨ãããã¯ãã«typedefãã¾ãã*1
次ã®ã«ã¼ãã§æ°ããç¹°ãè¿ãã¤ã³ããã¯ã¹ãæ°ããn
ã«åå®ç¾©ãããã®ã§nãundefãã¾ãã*2
ä¸è¦è¤éããã«è¦ãã¾ããã使ãã ããªãé£ããã¨ã¯ãã¦ãã¾ãããæ°´å¹³å復ã¨åç´å復ãé©åã«ä½¿ãåãã¦ããããã§ãã
*1:ä¸è¬ã«ãã¯ãã«ãããã£ãååä»ããã¤ããã®ã¯è¯ããªãã¨ããã¦ãã¾ãããããã¯ã°ãã¼ãã«ãã¯ãã®è©±ã§ããã¼ã«ã«ãã¯ãã§æå¾undefããããã®ããããã¤ã³ã¯ã«ã¼ããããã¡ã¤ã«ã§ä½¿ããªãã®ã§åé¡ããã¾ããã
*2:undefããªãã¨å ´åã«ãã£ã¦ã¯ãå®ç¾©æ¸ã¿ã®ãã¯ããåå®ç¾©ãããã¨ãã¦ãã¾ããã¨è¦åãåºãããããã¾ããã®ã§