How to write the best possible is_callable trait for templated operator()

后端 未结 2 879
挽巷
挽巷 2020-12-17 18:58

I have is_callable trait defined like this:

#ifndef IS_CALLABLE_HPP
#define IS_CALLABLE_HPP

#include 

namespace is_callable_detail
{
            


        
2条回答
  •  离开以前
    2020-12-17 19:07

    If you know in advance operator() is not going to be overloaded, you can try to take its address. If operator() is possibly overloaded, then a positive result would mean that there is an operator() present but a negative result would mean that either no operator() is present, or at least two overloads are.

    Notice that a template will (as expected) bring several overloads of operator(). However, if you do know the number of template parameters that are not defaulted you can try taking the address of operator() (for some type T that hopefully won't trigger SFINAE).

    As a final note, I'd suggest not trying to spend too much time trying to inspect functors (or member functions, for the same reasons) without knowing what arguments to pass, just like what you already have. C++11 makes it very easy to write and use generic code that functions at the expression level.

提交回复
热议问题