通过宏扩展调用方法

时间:2019-01-30 15:48:13

标签: c++ c++11 c++14

我有此代码:

class A {
public:
  bool has_foo() { return true; }
};

int main() {
  A a;

  CALL(a, foo);
}

我想通过宏扩展调用方法has_foo

#define CALL(object, method) do { object.has_ ## method ## (); } while(0)

上面的代码在MSVC上编译,但在GCC上失败。

我想使用宏扩展以避免运行时开销。

1 个答案:

答案 0 :(得分:3)

尝试

#define CALL(object, method) do { object.has_##method(); } while(0)
相关问题