Why do these C ++ cases create different patterns

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?

+4
source share
2 answers

++ void , ( C, ). , Ret(), Ret(Arg).

+5

void(void) , void() - void .

.

+3

All Articles