How to check if template argument is a callable with a given signature

后端 未结 5 1898
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 16:58

Basically, what I want to achieve is compile-time verification (with possibly nice error message) that registered callable (either a function, a lambda, a struct with call o

5条回答
  •  悲哀的现实
    2020-12-14 17:31

    Most of the answers are focused on basically answering the question: can you call the given function object with values of these types. This is not the same as matching the signature, as it allows many implicit conversions that you say you don't want. In order to get a more strict match we have to do a bunch of TMP. First, this answer: Call function with part of variadic arguments shows how to get the exact types of the arguments and return type of a callable. Code reproduced here:

    template 
    struct function_traits : public function_traits
    {};
    
    template 
    struct function_traits
    {
        using result_type = ReturnType;
        using arg_tuple = std::tuple;
        static constexpr auto arity = sizeof...(Args);
    };
    
    template 
    struct function_traits
    {
        using result_type = R;
        using arg_tuple = std::tuple;
        static constexpr auto arity = sizeof...(Args);
    };
    

    Having done that, you can now put a series of static asserts in your code:

    struct A {
      using Signature = void(int, double);
    
      template 
      void Register(Callable &&callable) {
        using ft = function_traits;
        static_assert(std::is_same>>::value, "");
        static_assert(std::is_same>>::value, "");
        static_assert(std::is_same>::value, "");
    
        callback = callable;
      }
    
      std::function callback;
    };
    

    Since you are passing by value this is basically all you need. If you are passing by reference, I would add an additional static assert where you use one of the other answers; probably songyuanyao's answer. This would take care of cases where for example the base type was the same, but the const qualification went in the wrong direction.

    You could of course make this all generic over the type Signature, instead of doing what I do (simply repeating the types in the static assert). This would be nicer but it would have added even more complex TMP to an already non-trivial answer; if you feel like you will use this with many different Signatures or that it is changing often it is probably worth adding that code as well.

    Here's a live example: http://coliru.stacked-crooked.com/a/cee084dce9e8dc09. In particular, my example:

    void foo(int, double) {}
    void foo2(double, double) {}
    
    int main()
    {
        A a;
        // compiles
        a.Register([] (int, double) {});
        // doesn't
        //a.Register([] (int, double) { return true; });
        // works
        a.Register(foo);
        // doesn't
        //a.Register(foo2);
    }
    

提交回复
热议问题