fanwenjie
fanwenjie

Reputation: 1

No matching overloaded function found C++ TEMPLATE

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)

Upvotes: 0

Views: 6220

Answers (1)

mpark
mpark

Reputation: 7904

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.

Upvotes: 6

Related Questions