加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 成员函数的std :: is_function?

发布时间:2020-12-16 10:02:17 所属栏目:百科 来源:网络整理
导读:我可以执行以下操作来检测某些内容是否为函数: void f(){}int main(){ std :: cout std :: is_function decltype(f) :: value std :: endl; // true} 现在,如果我想要做同样的事情,但是使用一个类的方法的函数会发生什么? 我天真地尝试做类似的事情 class
我可以执行以下操作来检测某些内容是否为函数:

void f()
{
}

int main()
{
  std :: cout << std :: is_function <decltype(f)> :: value << std :: endl; // true
}

现在,如果我想要做同样的事情,但是使用一个类的方法的函数会发生什么?

我天真地尝试做类似的事情

class myclass
{
public:
  void f()
  {
  }
};

int main()
{
  std :: cout << std :: is_function <decltype(myclass :: f)> :: value << std :: endl;
}

但我明白了

Call to non-static member function without an object argument

我应该做些什么?我想要像上面这样的东西……好吧,只打印真实.

解决方法

成员函数指针与函数的普通指针不同.另外,myclass :: f在没有&的情况下形成错误.对于成员函数,存在 std::is_member_function_pointer.

#include <iostream>
#include <type_traits>

class myclass
{
public:
   void f() {}
};

int main()
{
  std::cout << std::is_member_function_pointer<decltype(&myclass::f)>::value << std::endl;
}

Live on ideone

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读