Why these C++ cases instantiate different templates -
i trying write functionality need save different functions , later extract arguments' types. i'm using function signature template parameter. unexpected results. here's 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 variable somefoo
function type void(void)
, instantiates first template, foo<t>
. if change void(int)
, desired specialized template instantiated. why that?
in c++, having void
argument same having no argument @ (unlike in c, way). match specialization ret()
, can't match specialization ret(arg)
.
Comments
Post a Comment