Check if a class has a member function of a given signature

后端 未结 17 1732
無奈伤痛
無奈伤痛 2020-11-22 03:00

I\'m asking for a template trick to detect if a class has a specific member function of a given signature.

The problem is similar to the one cited here http://www.go

17条回答
  •  佛祖请我去吃肉
    2020-11-22 03:31

    This should be sufficient, if you know the name of the member function you are expecting. (In this case, the function bla fails to instantiate if there is no member function (writing one that works anyway is tough because there is a lack of function partial specialization. You may need to use class templates) Also, the enable struct (which is similar to enable_if) could also be templated on the type of function you want it to have as a member.

    template  struct enable { typedef T type; };
    template  typename enable::type bla (T&);
    struct A { void i(); };
    struct B { int i(); };
    int main()
    {
      A a;
      B b;
      bla(b);
      bla(a);
    }
    

提交回复
热议问题