Type trait: Check if class have specific function (maybe inherit)

前端 未结 3 1066
心在旅途
心在旅途 2021-01-05 07:49

I know that there are many possible ways to detect if a class has a specific function but non of them really work for my exact case. My current implementation to check for t

3条回答
  •  有刺的猬
    2021-01-05 08:38

    Here is a solution with or

    #include 
    #include 
    
    struct A {
      auto foo() { return 0; }
    };
    struct B {
      auto bar() { return 0.0; }
    };
    struct C : public A {
      auto bAr() { return 0.0; }
    };
    struct D : public C {
      auto baR() { return 0.0; }
    };
    
    template 
    using HasFoo_t = decltype(std::declval().foo());
    
    int main() {
      std::cout << std::experimental::is_detected_v << std::endl;
      std::cout << std::experimental::is_detected_v << std::endl;
      std::cout << std::experimental::is_detected_v << std::endl;
      std::cout << std::experimental::is_detected_v << std::endl;
    }
    

提交回复
热议问题