人ã ããã¯ãããã¡ã使ã£ã¦ããã§ããï¼ç½ ãç解ãã¦ã人ãå¤ãã¨ã¯æããªãã®ã§ããã¤ãæ¸ãã¾ãï¼
å®æ°å®ç¾©
constexpr
使ã£ã¦ãããï¼
#define INFLL 1e18 int main() { ... if (x == INFLL) return puts("-1"), 0; }
ã®ãããªã³ã¼ãã¯ç«¶ããã§ãããã¡ã§ããï¼ãã¶ãï¼ï¼
ããã¯ï¼x == 999999999999999999
ï¼ã¤ã¾ã \(10^{18}-1\) ã®ã¨ãã« double
ã®èª¤å·®ã®é¢ä¿ã§ -1
ãåºåããã¦ãã¾ãã¾ãï¼
ç´ ç´ã«ä»¥ä¸ã®ããã«æ¸ãã¾ãããï¼
constexpr long long INFLL = 1e18; int main() { ... if (x == INFLL) return puts("-1"), 0; }
ãã£ã¹ããã¦ã¾ã§ #define
ããã人ã¯ããããããªãï¼æã人ã£ã½ãï¼
#define INFLL ((long long)1e18)
ãã³ãã¬ã¼ãã¨ãããã¨ãã« constexpr
ã¾ããã§æå³ä¸æãçãããããã®ã§ï¼æèåæ¢ã§ã©ãã«ãããæ段ã¨ãã¦ãã¯ãããªãã¯ãªããã§ããï¼ããã¯ãªãã¨æãï¼
è¤æãã¯ã
ããã¯æåãã¿ã ã¨æã£ã¦ããã§ããï¼ç«¶ãã er ã®ãã³ãã¬ã¼ããè¦ã¦ãã¨æå¤ã¨ç¥ããã¦ããªããããªã®ã§æ¸ãã¦ããã¾ãï¼
#define YES(x) printf("Yes %d", x); return 0;
è¤æ°ã®æãããªããã¯ãããã®ã¾ã¾æ¸ãã®ã¯æå¾ ããªããã¨ã«ãªããã¡ãªã®ã§æ³¨æããããã¨ãã話ã§ãï¼
int main() { int x; scanf("%d", &x); if (x == 1) YES(1); ... // x != 1 ã®ã±ã¼ã¹ã§ããããããã°ã }
ããããã°ããã¨ã¯ãããããã¾ããï¼ ããããªã人ã¯ãã¯ã使ãã®é¿ããæ¹ãããã¨æããã§ããã©...
ãã¼ï¼ç¥ããªãã£ããã§ããããã³ã³ãã¤ã«æè¦ååºããã§ããï¼[-Wmultistatement-macros]
if (x == 1) printf("Yes %d\n", 1); return 0;;
ã®é¨åãããããããã¤ã³ãã³ãããã¨æ¬¡ã®ããã«ãªãã¾ããï¼
if (x == 1) printf("Yes %d\n", 1); return 0; ;
ã¾ãããã¯å½ç¶ãã°ãï¼ ç¥ããªã人ã¯ãgoto failãã¨ãã§ã°ã°ãã¨é¢ç½ãããããã¾ããï¼
ãããï¼æ¬¡ã®ããã«æ¸ãã°å®å ¨ã§ããï¼
#define YES(x) { printf("Yes %d", x); return 0; }
ãããï¼ã ãï¼ï½
if (foo) YES(x); else ...
ã¨ãæ¸ãã¦ã¿ã¾ãããï¼ããã¯æ¬¡ã®ããã«ãªãã¾ãï¼
if (foo) { printf("Yes %d\n", x); return 0; } ; else ...
else
ã if
ã«å¯¾å¿ãã¦ããªãã¦ã ãã«ãªãã¾ããï¼
å¤ãããä¼ããã¤ãã£ãªã ãç´¹ä»ãã¾ãï¼æ¬¡ã®ããã«æ¸ãã¨å®å¿ã§ãï¼
#define YES(x) do { \ printf("Yes %d\n", x); \ return 0; \ } while (false)
do-while æã®æå¾ã«ã»ãã³ãã³ãç½®ãã¦ããªãã®ããã¤ã³ãã§ï¼ãã¯ãã使ãå´ã§ã»ãã³ãã³ãæ¸ããããã«ãªãã¾ãï¼
if (foo) YES(x); else ...
ã¨ãã¦ãï¼
if (foo) do { printf("Yes %d\n", x); return 0; } while (false); else ...
ã¨ãªãï¼ããæãã«ãªãã¾ãï¼
ã¨ãããã¨ã§ï¼è¤æ°ã®æãããªããã¯ãã使ãããã¨ã㯠do { ... } while (false)
ã®ä¸ã«å
¥ããããã«ãã¾ãããï¼
GCC ã®æ¡å¼µã許ãã®ã§ããã°ï¼ä»¥ä¸ã®ããã«æ¸ããã¨ãã§ãã¾ãï¼
#define f(x) (void)({ \ ... \ })
ãã£ã¡ã®æ¹ãè¦ãç®ã¯ç°¡åã§ããï¼é¢æ°ãã¯ããã«ã³ãåºåãã§ä½¿ãããã¨ãã¯ãã£ã¡ã®æ¹æ³ãããªãã¨ã ãã ã¨æãã¾ãï¼
ãã¯ãã®ç¨æ³
ãããããã¯ãã¯åºæ¬çã«ä½¿ãã¹ãã§ã¯ãªãã¦ï¼ã§ããéãé¢æ°ãã³ãã¬ã¼ãããªããã§ããã¹ãã§ãããï¼
å¯ä½ç¨ã®åæ°
ãã¨ãã°ï¼ä»¥ä¸ã®ãããªé¢æ°ãã¯ããèãã¾ãï¼
#define f_macro(x, y) y = std::max(g(x), h(x))
é¢æ°ãã³ãã¬ã¼ãã§ã¯ãããªæãã§ãï¼
template <typename Tp> void f_template(Tp const& x, Tp& y) { y = std::max(g(x), h(x)); }
ãã¦ï¼æ´æ°ãæ¨æºå
¥åããåãåãé¢æ° read_int()
ã x
ã«æ¸¡ããã¨ãèãã¦ã¿ã¾ããããï¼
f_template(read_int(), y);
ã§ã¯å¼ã³åºãæã«ä¸ã¤æ´æ°ãåãåãã¾ãï¼ä¸æ¹ï¼
f_macro(read_int(), y);
ã§ã¯æ´æ°ãäºåèªã¿è¾¼ã¾ããï¼ãããåã
ã®æ´æ°ã g
㨠h
ã®ã©ã¡ãã«æ¸¡ããã¯æªè¦å®ï¼ãã¨ã«ãªãã¾ããï¼å±éããã¨æ¬¡ã®ããã«ãªã£ã¦ãã¾ãããã§ãï¼
y = std::max(g(read_int()), h(read_int()));
ãã¯ãé¢æ°ã®å ´åã¯å¼æ°ãäºå以ä¸ä½¿ããªãããã«æ°ãã¤ããï¼ã¨ãããã¡ããã©ä¸åã®æ¹ãããã®ããªï¼ã¨ããããã¨äºæããªããã¨ã«ãªãã«ããã§ãï¼ãããªãã¨ã«æ°ãæã£ã¦ã¾ã§ãã¯ãã使ãããã®ã¯ã¡ãã£ã¨è¬ã§ããã©ï¼
æ¼ç®åã®åªå é åº
#define f_macro(x, y) x * y template <typename Tp> Tp f_template(Tp const& x, Tp const& y) { return x * y; }
a+1
㨠b+1
ã渡ãã¦ã¿ã¾ãããï¼
f_macro(a+1, b+1); // a+1 * b+1 == a + (1*b) + 1 f_template(a+1, b+1); // (a+1) * (b+1)
ãã¯ãã®æ¹ã¯æå¾ ããåä½ã«ãªã£ã¦ãã¾ããï¼ ãã¡ããæ¬å¼§ã§ãããã°åé¡ãªããã§ããï¼äººã¯å½ç¶å¿ããã®ã§ãã°ã®å ã§ãï¼
ä¾å¤
é¢æ°ãã³ãã¬ã¼ãã§ä»£æ¿ã§ããªããããªï¼æ§æã¬ãã«ã§ã©ãã«ããããå ´åã«éã£ã¦ãã¯ãã使ãã®ãããã¨æãã¾ãï¼ç«¶ããã§ãããã rep ãã¯ãã¿ãããªãã¤ã§ããï¼
ä»ã«ã ##
æ¼ç®åã¨ãï¼ããããã®ã使ãããå±é¢ã¨ãããªãï¼
ãã¼ã¯ã³ãçµåããããã¤ã§ãï¼
#define f(a, b) a ## b f(ho, ge) // hoge ã«ãªã
ã«ããã«ãã
ãã³ã¡ããã¯ææãªã®ã§ããã®ã³ã¼ãã¯ããããå±é¢ã§æå¾ ããªããã¨ã«ãªãã§ãããã¨ããã®ãããæ°ã«ãã¡ãã*1ãã§ããï¼ããããå¤ãã®äººã¯ããã¾ã§ãã°ãã³ã¼ãã¯æ¸ããªããã§ãããï¼ãã¶ãï¼ï¼
ããã¯è¨ã£ã¦ãï¼å±éºãèµ·ããã«ããã³ã¼ããæ¸ãããã«å¿ããã¦ããæ¹ãããããããªãããªãã¨ãã³ã¡ããçã«ã¯æã£ã¦ãã¾ãï¼
*1:çãã¥ãããã£ã¦è¨ãããå´ã®äººéã£ã½ãï¼ã¡ãã£ã¨çãã¥ããï¼