如何检查成员名称(变量或函数)是否存在于类中(指定或不指定类型)? [英] How to check if a member name (variable or function) exists in a class, with or without specifying type?

查看:166
本文介绍了如何检查成员名称(变量或函数)是否存在于类中(指定或不指定类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此Q是以下内容的扩展:
是否可以编写C ++模板来检查函数的存在?

This Q is an extension of:
Is it possible to write a C++ template to check for a function's existence?

是否有任何实用工具可以帮助您找到:

Is there any utility which will help to find:

  • 班级中是否存在成员名称?该成员可以是 变量或方法.
  • 指定成员的类型应该是可选的
  • If a member name exists inside a class or not? This member can be a variable or a method.
  • Specifying the type of the member should be optional

推荐答案

C ++ 03

#define HasMember(NAME) \
  template<class Class, typename Type = void> \
  struct HasMember_##NAME \
  { \
    typedef char (&yes)[2]; \
    template<unsigned long> struct exists; \
    template<typename V> static yes Check (exists<sizeof(static_cast<Type>(&V::NAME))>*); \
    template<typename> static char Check (...); \
    static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  }; \
  template<class Class> \
  struct HasMember_##NAME<Class, void> \
  { \
    typedef char (&yes)[2]; \
    template<unsigned long> struct exists; \
    template<typename V> static yes Check (exists<sizeof(&V::NAME)>*); \
    template<typename> static char Check (...); \
    static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  }

用法:只需使用要查找的任何成员调用宏:

Usage: Simply invoke the macro with whatever member you want to find:

HasMember(Foo);  // Creates a SFINAE `class HasMember_Foo`
HasMember(i);    // Creates a SFINAE `class HasMember_i`

现在,我们可以使用HasMember_X来检查ANY class中的X,如下所示:

Now we can utilize HasMember_X to check X in ANY class as below:

#include<iostream>
struct S
{
  void Foo () const {}
//  void Foo () {}  // If uncommented then type should be mentioned in `HasMember_Foo`    
  int i;
};
int main ()
{
  std::cout << HasMember_Foo<S, void (S::*) () const>::value << "\n";
  std::cout << HasMember_Foo<S>::value << "\n";
  std::cout << HasMember_i<S, int (S::*)>::value << "\n";
  std::cout << HasMember_i<S>::value << "\n";
}

捕获:

  1. 对于方法,如果我们不提及类型,则class 一定不能有重载的方法.如果有,那么这个技巧就失败了. 即,即使指定的成员存在多次,结果仍为false.
  2. 如果成员是基类的一部分,则此技巧失败;否则,此技巧将失败.例如如果BS的基数& void B::Bar ()存在,然后HasMember_Bar<S, void (B::*)()>::valueHasMember_Bar<S, void (S::*)()>::valueHasMember_Bar<S>::value将给出false
  1. In case of methods, if we don't mention the type then the class must not have overloaded methods. If it has then this trick fails. i.e. even though the named member is present more than once, the result will be false.
  2. If the member is part of base class, then this trick fails; e.g. if B is base of S & void B::Bar () is present, then HasMember_Bar<S, void (B::*)()>::value or HasMember_Bar<S, void (S::*)()>::value or HasMember_Bar<S>::value will give false

这篇关于如何检查成员名称(变量或函数)是否存在于类中(指定或不指定类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆