Pav*_*vel 4 c++ templates template-specialization function-signature
我正在尝试编写一些功能,我需要保存不同的函数,然后提取它们的参数类型.所以我使用函数签名作为模板参数.但我得到一些意想不到的结果.这是代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果变量someFoo是具有类型的函数void(void),则它实例化第一个模板,foo<T>.但是,如果我将其更改为void(int),则会实现所需的专用模板.这是为什么?
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |