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

前端 未结 5 790
北海茫月
北海茫月 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:57

    With

    #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature)               \
        template                                                    \
        class traitsName                                                        \
        {                                                                       \
        private:                                                                \
            template struct helper;                              \
            template                                                \
            static std::uint8_t check(helper*);           \
            template static std::uint16_t check(...);               \
        public:                                                                 \
            static                                                              \
            constexpr bool value = sizeof(check(0)) == sizeof(std::uint8_t); \
        }
    
    DEFINE_HAS_SIGNATURE(has_ham_const, T::ham, void (T::*)() const);
    

    And then

    static_assert(has_ham_const::value, "unexpected");
    static_assert(!has_ham_const::value, "unexpected");
    

    Demo

提交回复
热议问题