如何检查类型是否存在无参数操作符()

Howto check a type for the existence of parameterless operator()

本文关键字:存在 参数 操作符 是否 类型 检查 何检查      更新时间:2023-10-16

我试图检查一个函子是否与给定的一组参数类型和给定的返回类型兼容(也就是说,给定的参数类型可以隐式地转换为实际的参数类型,而返回类型则相反)。目前我使用以下代码:

    template<typename T, typename R, template<typename U, typename V> class Comparer>
    struct check_type
    { enum {value = Comparer<T, R>::value}; };
    template<typename T, typename Return, typename... Args>
    struct is_functor_compatible
    {
        struct base: public T
        {
            using T::operator();
            std::false_type operator()(...)const;
        };
        enum {value = check_type<decltype(std::declval<base>()(std::declval<Args>()...)), Return, std::is_convertible>::value};
    };

check_type<T, V, Comparer>这在大多数情况下工作得很好,但是当我测试像struct foo{ int operator()() const;};这样的无参数函子时,它无法编译,因为在这种情况下,base的两个operator()显然是含糊不清的,导致如下所示:

error: call of '(is_functor_compatible<foo, void>::base) ()' is ambiguous
note: candidates are:
note: std::false_type is_functor_compatible<T, Return, Args>::base::operator()(...) const [with T = foo, Return = void, Args = {}, std::false_type = std::integral_constant<bool, false>]
note: int foo::operator()() const

显然我需要一种不同的方法来检查无参数函子。我尝试为空参数包对is_functor_compatible进行部分专门化,在那里我检查&T::operator()的类型是否为无参数成员函数,它或多或少地起作用。然而,当测试函子有多个operator()时,这种方法显然失败了。

因此,我的问题是是否有更好的方法来测试无参数operator()的存在以及如何做到这一点。

当我想测试给定表达式是否对某个类型有效时,我使用类似于下面的结构:

template <typename T>
struct is_callable_without_parameters {
private:
    template <typename T1>
    static decltype(std::declval<T1>()(), void(), 0) test(int);
    template <typename>
    static void test(...);
public:
    enum { value = !std::is_void<decltype(test<T>(0))>::value };
};

您试过这样做吗:

template<size_t>
class Discrim
{
};
template<typename T>
std::true_type hasFunctionCallOper( T*, Discrim<sizeof(T()())>* );
template<typename T>
std::false_type hasFunctionCallOper( T*, ... );

之后,对返回类型进行区分hasFunctionCallOper((T*)0, 0) .

编辑(感谢R. Martinho Fernandes的建议):

下面是有效的代码:

template<size_t n>
class CallOpDiscrim {};
template<typename T>
TrueType hasCallOp( T*, CallOpDiscrim< sizeof( (*((T const*)0))(), 1 ) > const* );
template<typename T>
FalseType hasCallOp( T* ... );
template<typename T, bool hasCallOp>
class TestImpl;
template<typename T>
class TestImpl<T, false>
{
public:
    void doTellIt() { std::cout << typeid(T).name() << " does not have operator()" << std::endl; }
};
template<typename T>
class TestImpl<T, true>
{
public:
    void doTellIt() { std::cout << typeid(T).name() << " has operator()" << std::endl; }
};
template<typename T>
class Test : private TestImpl<T, sizeof(hasCallOp<T>(0, 0)) == sizeof(TrueType)>
{
public:
    void tellIt() { this->doTellIt(); }
};