How do I pass std::plus as an argument across functions?
#include<functional>
#include<iostream>
template < class T, typename F >
T fn(T a, T b, F f)
{
return f<T>()(a,b);
}
template<class T>
struct X
{
template < typename F>
T foo(T a, T b, F f)
{
return fn<T, F>(a,b,f);
}
};
int main()
{
int a = 1;
int b = 1;
X<int> f;
std::cout<< f.foo(a,b, std::plus<int>);
return 0;
}
https://onlinegdb.com/g5NZc2x9V
main.cpp:7:21: error: expected primary-expression before ‘)’ token
std::cout<< f.foo(a,b, std::plus<int>);
std::plus<int> is a type. Just like int. This fails for the exact same reason the following code would fail to compile, too:
void function(int n)
{
}
void another_function()
{
function(int);
}
You can't "pass std::plus as an argument" for the same exact reason you can't pass int as an argument.
What you can do, though -- and what you should do --- is to construct an instance of std::plus:
std::cout<< f.foo(a,b, std::plus<int>{});
Now, this passes an actual object, std::plus<int> to this function. However this is not going to be sufficient:
T fn(T a, T b, F f)
{
return f<T>()(a,b);
}
Here, f is going to be that std::plus<int> object. It is a callable object, so, well, all you have to do is call it:
T fn(T a, T b, F f)
{
return f(a,b);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With