【问题标题】:Check member functions signature in compile time [duplicate]在编译时检查成员函数签名[重复]
【发布时间】:2013-01-27 14:33:46
【问题描述】:

可能重复:
Is it possible to write c++ template/macros to check whether two functions have the same signatures

是否可以编写c++模板/宏来检查两个成员函数在编译时是否具有相同的签名(返回类型和参数列表)?

我想要这样的东西:

CHECK_SIGNATURES(Foo, foo, Bar, bar);

如果 Foo::foo 和 Bar::bar 函数具有不同的返回类型或参数列表,则编译失败。

【问题讨论】:

  • 昨天对非成员函数提出了同样的问题,同样的答案也适用。
  • 请举例说明你想达到什么目的
  • 链接到重复问题不能解决 MEMBER 函数的匹配问题。会员匹配解决方案在stackoverflow.com/questions/51408881/…

标签: c++ templates macros


【解决方案1】:

尝试以下方法:

template <class T>
bool same(T, T) { return true; }

template <class T, class U>
bool same(T, U) { return false; }

或者您可以使用std::is_same,就像在duplicate question 的答案中一样。

【讨论】:

  • @leemes 不,它会同时检查 -- liveworkspace.org/code/4yc7Hy$758
  • 忘记我说的话。就是这么想的,还是谢谢你指出来。
  • @MarcGlisse 我没有从任何人那里复制这个...
  • 通过将其设为具有static const bool value 的类,使其成为类型特征。或者这个版本仍然可以用于编译时表达式(constexpr)吗? C++03 呢?
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 2015-07-31
  • 2011-04-27
相关资源
最近更新 更多