此站点上有其他答案使用SFINAE但非C++ 11代码,还有其他人使用C++ 11代码(如decltypes)来简化此过程.但是,我不确定如何检查类是否具有特定签名的函数.
我想检查一个类是否具有函数receive(const Event &)where Event在调用check函数时指定的类类型.
我知道的最好的方法是检查你是否可以实际调用该函数,如果它返回你期望的类型.这是一个如何检测类C是否具有作为参数和"返回" 的receive方法的示例.检测并不关心是否该方法是在类实现直接或以某些基类派生自,它也不关心是否有进一步拖欠参数.根据需要进行调整.const Event&voidCC
template< typename C, typename Event, typename = void >
struct has_receive
: std::false_type
{};
template< typename C, typename Event >
struct has_receive< C, Event, typename std::enable_if<
std::is_same<
decltype( std::declval<C>().receive( std::declval<const Event&>() ) ),
void
>::value
>::type >
: std::true_type
{};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2441 次 |
| 最近记录: |