Effective C++ - 条款2 - 使用inline template函数替换函数宏

为什么?

即使给函数宏加上了小括号,如下

#define F(a,b) f((a)>(b)?(a):(b))

但仍会在使用++运算符时产生问题

F(++a,b)
// 替换为
f((++a)>(b)?(++a):(b))

怎么做

永远使用inline template替换函数宏

template <typename T>
inline void F(const T& a, const T& b)
{
f(a>b?a:b);
}
posted @ 2022-08-17 23:26  ijpq  阅读(45)  评论(0编辑  收藏  举报