Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved overloaded function type while calling template member function in a class template

Tags:

c++

templates

Consider the following code:

struct Test {
    template <int S>
    bool call();
};

template <>
bool Test::call<0>() {
    return false;
}

template <>
bool Test::call<1>() {
    return true;
}

template <int S, typename T>
static void func(T& t) {
    t.call<S>();
}

int main()
{
    Test t;
    func<0>(t);
}

I got a compilation error:

a.cpp: In function ‘void func(T&)’:
a.cpp:19:15: error: expected primary-expression before ‘)’ token
a.cpp: In instantiation of ‘void func(T&) [with int S = 0; T = Test]’:
a.cpp:25:14:   required from here
a.cpp:19:5: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’

If I put t.call<0>() or t.call<1>() in the main() function, it works fine. Can someone tell me why template argument deduction doesn't work for this code? I am not sure why passing in a type with a partially-specialized template member function won't work in this case.

like image 251
user2952004 Avatar asked Sep 02 '26 18:09

user2952004


1 Answers

You need to say

template <int S, typename T>
static void func(T& t) {
    t.template call<S>();
}

Because T is a dependent type name, the compiler doesn't know that call() is a template function, so you have to make it clear.

like image 191
Tristan Brindle Avatar answered Sep 04 '26 06:09

Tristan Brindle