在模板中,如果从属名称是函数,则调用它

Vox*_*Box 6 c++ templates sfinae type-traits c++11

在我的TClass<T>::foo()函数中,我想调用一个T实例,当且仅当T它是函数类型时.

#include <iostream>
#include <functional>

template<class T>
struct TClass
{
    TClass(T value) : value(value) {}
    T value;
    void foo()
    {
        // if(value is std::function)
        //     call function;
    }
};

int main()
{
    TClass<int> t1{0};
    t1.foo();
    TClass<std::function<void()>> t2{[](){ std::cout << "Hello, World!\n"; }};
    t2.foo();
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Bar*_*rry 7

在C++ 11中,最简单的方法是通过辅助函数重新推导出值:

template <typename U>
auto foo_helper(U const& f, int) -> decltype(f()) {
    return f();
}

template <typename U>
void foo_helper(U const&, long) {}

void foo() {
    foo_helper(value, 0);
}
Run Code Online (Sandbox Code Playgroud)

转换为0to int比转换为更好long,因此如果第一次过载是可行的 - 它将是首选.如果第一次过载不可行,那么我们称之为第二次过载.


如果你真的关心std::function,那么我们可以有更简单的重载:

void foo_helper(std::function<void()> const& f) {
    f();
}

template <typename T>
void foo_helper(T const&) { }

void foo() {
    foo_helper(value);
}
Run Code Online (Sandbox Code Playgroud)