How to detect the presence of a static member function with certain signature?

前端 未结 3 596
一生所求
一生所求 2020-12-18 11:08

I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. How

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 11:18

    Following may help: (https://ideone.com/nDlFUE)

    #include 
    
    #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); \
        }
    

    Then define a traits:

    DEFINE_HAS_SIGNATURE(has_foo, T::foo, void (*)(void));
    

提交回复
热议问题