我正在尝试编写一些需要保存不同函数并稍后提取其参数类型的功能。所以我使用函数签名作为模板参数。但我得到了一些意想不到的结果。这是代码:
#include <functional>
#include <iostream>
template <class T>
struct foo
{
    foo()
    {
        std::cout << "class T" << std::endl;
    }
};
template <class Ret, class Arg>
struct foo<Ret(Arg)>
{
    foo()
    {
        std::cout << "Ret(Arg)" << std::endl;
    }
};
template <class T>
void save(std::function<T>)
{
    new foo<T>();
}
int main(int argc, char* argv[])
{
    std::function<void(void)> someFoo;
    save(someFoo);
    return 0;
}
因此,如果变量someFoo是一个类型为 的函数void(void),它会实例化第一个模板foo<T>. 但是,如果我将其更改为void(int),那么我将获得所需的专用模板实例化。这是为什么?