如何检查静态函数的存在

时间:2014-04-11 17:15:45

标签: c++ templates c++11 sfinae

我想检查是否存在带有指定参数的静态方法。我找到了一个仅适用于非静态函数的解决方案(使用msvc-2013和mingw32-gcc 4.8.1)。

template<typename T, typename Res, typename... Args>
class HasFunc
{
    template <typename U, Res (U::*)(Args...)> struct Check;
    template <typename U> static true_type func(Check<U, &U::func> *);
    template <typename U> static false_type func(...);
public:
    static const bool value = decltype(func<T>(0))::value;
};

template<typename T>
struct Test{
    static void func(const T&, T); // version 1
    ////// void func(const T&, T); // version 2
};

static_assert(HasFunc<Test<int>, void, const int&, int>::value, "");
// fails in version 1, not in version 2

我不知道应该改变什么。你能救我吗?

0 个答案:

没有答案