How can I check whether a member function has const overload?

前端 未结 5 792
北海茫月
北海茫月 2021-01-19 05:30

Lets say I have

struct foo {
    void ham() {}
    void ham() const {}
};

struct bar {
    void ham() {}
};

Assuming I have a templated fu

5条回答
  •  情书的邮戳
    2021-01-19 05:38

    Detector (like is_detected):

    template 
    using void_t = void;
    
    template  class D, typename = void>
    struct detect : std::false_type {};
    
    template  class D>
    struct detect>> : std::true_type {};
    

    Sample member verifier:

    template 
    using const_ham = decltype(std::declval().ham());
    

    Test:

    static_assert(detect::value, "!");
    static_assert(!detect::value, "!");
    

    DEMO

提交回复
热议问题