Reputation: 973
I'm trying to call a templated member function using std::invoke
as in the code snipped below.
#include <functional>
#include <iostream>
struct Node
{
template <typename T>
T eval(T lhs, T rhs) { return lhs + rhs; }
};
int main(void)
{
std::cout << std::invoke(&Node::eval<int>, 1, 2);
return 0;
}
gcc 8.1 is giving me the error
no matching function for call to 'invoke(unresolved overloaded function type, int, int)'
which I think it means that the function template is not actually instantiated.
What am I missing to make the call possible?
Upvotes: 2
Views: 1094
Reputation: 13434
The problem is that you try to call a member function with no objects. There are two solutions:
eval
as static
.Create a Node
object and std::invoke
the eval
method on that object. It would look like this:
int main()
{
Node n{};
std::cout << std::invoke(&Node::eval<int>, n, 1, 2);
return 0;
}
Here, the n
object is passed as a object for this
pointer to eval
method.
Upvotes: 3
Reputation: 10425
Two problems:
First, your function template takes one type parameter, not two.
Second, your function is not static
, hence you can't use it without an object.
struct Node
{
template <typename T>
static T eval(T lhs, T rhs) { return lhs + rhs; }
^^^^^^
};
int main(void)
{
std::cout << std::invoke(&Node::eval<int>, 1, 2);
^^^
return 0;
}
Upvotes: 3