具有默认类型和typedef的模板参数推导(C ++)

时间:2018-11-08 21:19:31

标签: c++

为什么callB(f)编译而callA(f)不编译?他们不是一样吗? 我已经尝试过使用gcc和MSVC ...相同的结果。

#include <functional>

template<typename T>
struct Function
{
    typedef std::function<void(T)> type;
};

template<typename T = int>
void callA(std::function<void(T)> f)
{
    f(T());
}

template<typename T = int>
void callB(typename Function<T>::type f)
{
    f(T());
}

int main()
{
    auto f = [](int) { };

    callA(f);    // compile error: "template argument deduction/substitution failed: main()::<lambda(int)>’ is not derived from ‘std::function<void(T)>"
    callB(f);    // compiles

    return 0;
}

0 个答案:

没有答案
相关问题