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

后端 未结 17 1739
無奈伤痛
無奈伤痛 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:19

    With c++ 20 this becomes much simpler. Say we want to test if a class T has a member function void T::resize(typename T::size_type). For example, std::vector has such a member function. Then,

    template
    concept has_resize_member_func = requires {
        typename T::size_type;
        { std::declval().resize(std::declval()) } -> std::same_as;
    };
    

    and the usage is

    static_assert(has_resize_member_func, "");
    static_assert(has_resize_member_func == false, "");
    

提交回复
热议问题