Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why these C++ cases instantiate different templates

I am trying to write some functionality where I need to save different functions and later extract their arguments' types. So I'm using the function signature as template parameter. But I get somewhat unexpected results. Here's 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 someFoo is a function with type void(void), it instantiates the first template, foo<T>. But if I change it to void(int), than I get the desired specialized template instantiated. Why is that?

like image 698
Pavel Avatar asked Feb 10 '23 07:02

Pavel


1 Answers

In C++, having a void argument is actually the same as having no argument at all (unlike in C, by the way). So it would match a specialization for Ret(), but it can't match a specialization for Ret(Arg).

like image 170
Arkanosis Avatar answered Feb 13 '23 04:02

Arkanosis