I am trying to write some functionality where I need to store various functions and then extract their argument types. Therefore, I use the function signature as a template parameter. But I get some unexpected results. Here is the code:
#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;
}
So, if the variable someFoois a function of the type void(void), it creates a first template foo<T>. But if I change it to void(int), I will get the required specialized template. Why is this?
Pavel source
share