0
votes

Recurvise call C++ template function with Variable template

void int void foo()
{

}

template <typename T, typename ...U> void foo()
{
    foo<U...>();
}

int main()
{
    foo<int, char, int>();
    return 0;
}

The compile meesage such as:

error C2672: 'foo': no matching overloaded function found note: see reference to function template instantiation 'void foo(void)' being compiled note: see reference to function template instantiation 'void foo(void)' being compiled note: see reference to function template instantiation 'void foo(void)' being compiled error C2783: 'void foo(void)': could not deduce template argument for 'T' note: see declaration of 'foo'

I declare the void foo(void), Why the error occured? complier can match template void foo(), but can't match void foo(void)

1
"void int void foo()" is not valid C++. The error message does not match the code. Try again.Sam Varshavchik
The template version foo<U...>() require a specialization to stop the recursion. void foo() is not a specialization of the template.qduyang

1 Answers

6
votes

Assuming your base case is void foo() {}, in your recursive case you're performing the following function invocations:

foo<int, char, int>();
foo<char, int>();
foo<int>();
foo<>();

Notice that the last invocation is foo<>(); rather than foo();. The compiler error is due to the fact that your base case of void foo() {} cannot be called with the foo<>(); syntax.