user2952004
user2952004

Reputation: 31

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

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.

Upvotes: 3

Views: 957

Answers (3)

Tristan Brindle
Tristan Brindle

Reputation: 16824

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.

Upvotes: 5

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153810

It looks as if you wanted to write

t. template call<S>();

The name call is clearly dependent and, thus, not considered to be template unless it is explicitly stated that it is a template. Currently, I can't easily check if this is this is the only issue.

Upvotes: 1

greatwolf
greatwolf

Reputation: 20838

You need to disambiguate the parse using template keyword:

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

Upvotes: 2

Related Questions