How can i do some thing like:
#include <functional>
#include <boost/functional.hpp>
int foo(int){return 1;};
template<typename T>
int bar(T t)
{
return 10;/*or something*/
}
int main() {
bar< std::ptr_fun<int, int>(foo) > (1);
bar< boost::ptr_fun<int, int>(foo) > (1);
return 0;
}
In both ptr_fun-lines i got error C2974: 'bar' : invalid template argument for 'T', type expected. As far as i know prt_fun creates a type, but std::ptr_fun<int, int>(foo) creates an object.
Is there way to create a type “initialized” with function pointer usinf as much std as possible?
Probably could solve this by manualy coding an functor, but i belive there is the ptr_fun-way.
ptr_funreturns an object of type pointer_to_unary_function. You declared your template to take a type parameter, so passing it an object clearly won’t work.You could make it work like this (note you don’t need to specify the template parameter, it can be deduced by the compiler):
But you don’t really need
ptr_fun. You could simply do it like this:Or, to make it work like you set it up:
Lots of quesswork, becase it’s not really clear what you’re trying to accomplish.