enable_if template param is lambda (with particular signature)

前端 未结 1 755
我寻月下人不归
我寻月下人不归 2021-01-21 14:10

I have something this:

template
class Image {
    Image(int w, int h, T defaultVal){
        for(int i=0; i

        
相关标签:
1条回答
  • 2021-01-21 14:55

    You're looking for std::is_invocable:

    template<typename F, typename = std::enable_if_t<
        std::is_invocable<F&, int, int>>
    

    F& because you're invoking it as an lvalue, and then after that it's just a list of types of parameters.


    The above requires C++17. It is implementable on C++14, but in your case we can also take a much simpler approach and just do:

    template <typename F, typename = decltype(std::declval<F&>()(1, 1))>
    

    F& for the same reason as above, and the rest of the expression is more familiar. Since we're calling with ints we don't care about the other things that INVOKE allows for (e.g. pointers to members).

    0 讨论(0)
提交回复
热议问题