I try to pass std::log as a functional argument, but it seems that there are overloaded implementations of std::log and the compiler failed to resolve it. Codes:
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <functional>
template <typename FOper>
double Eval(FOper fOper, double X)
{
return fOper(X);
}
int main(int argc, char* argv[])
{
std::function<double(double)> fPlus1 = std::bind(std::plus<double>(), 1.0, std::placeholders::_1);
std::cout<<Eval(fPlus1, 10.0)<<std::endl;
// how to write this fLog ?
//std::function<double(double)> fLog = std::log;
//std::function<double(double)> fLog = std::log<double>;
std::cout<<Eval(fLog, 10.0)<<std::endl;
return 0;
}
The complier prompts an error message if I uncomment either line of the definition of fLog:
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<double(doubl
e)>' requested
The easiest way would be to simply cast it:
typedef double (*log_d)(double);
std::function<double(double)> fLog = static_cast<log_d>(std::log);
With the cast, you hand the compiler a context in which the overloaded function is used, and as such will get the correct function pointer out of it.
Like Xeo explained, it is possible to get it to work even when the function is overloaded using an explicit cast. However, since you're using std::function already (which is a C++11 feature), you might as well just use a lambda expression as initializer:
function<double(double)> fLog = [](double x){return std::log(x);};
This is preferable in C++11 because it avoids overloading issues. Also, it is more efficient than wrapping a function pointer because it saves one level of indirection and therefore allows the lambda's body to be inlined into the internal wrapper object's function call operator.
It should probably be stressed that the use of std::function in your example is unnecessary since Eval is already a function template and the type parameter FOper can exactly match the type of the function object without the need to wrap it inside std::function. So, if you don't need the type erasure you get by using std::function, you could just as well write
template <typename FOper>
double Eval(FOper fOper, double X)
{
return fOper(X);
}
int main()
{
auto flog = [](double x){return std::log(x);};
std::cout << Eval(flog, 10.0) << std::endl;
}
You do this:
typedef double (*logtype)(double);
std::function<double(double)> fLog = (logtype) std::log;
The cast will help compiler to select the correct overload.
You can also write this:
double (*fLog )(double) = std::log; //i.e don't use std::function
std::cout<<Eval(fLog, 10.0)<<std::endl;
The issue is that something like this on its own has no meaning:
std::bind( std::log, _1 ); // cannot resolve. bind what function? What will _1 be passed as?
log is not a template so you can't call std::log<double> in there.
You can make your own template though, that will resort to log:
template< typename T >
T logT( T t )
{
return std::log( t );
}
and now you can use logT in your code.
std::bind( logT<double>, _ 1 ) // should work.
You could of course make fLog a function pointer to logT if you want. With C++11 you can use auto etc. to not have to type out its type by hand.
Related
In this code, how can I use decltype in std::future to deduce the return type of bar() ? Although directly using std::future<int> works, I would like to know how can decltype be used in such a situation.
#include <iostream>
#include <future>
int bar(int a)
{
return 50;
}
int main()
{
std::packaged_task<decltype(bar)> task(bar);
//std::future<decltype(bar(int))> f = task.get_future(); //doesn't work need to do something like this
std::future<int> f = task.get_future(); //works
std::thread t1(std::move(task), 10);
t1.detach();
int val = f.get();
std::cout << val << "\n";
return 0;
}
Also, is the use of decltype in std::packaged_task correct ?
Note that you can use auto:
auto f = task.get_future();
And everything works as expected.
decltype is used to detect the type of an expression. In this case, bar(int) is not a valid expression. You may use decltype(bar(0)).
Alternatively, you can use the dedicated tools for determining the result of a function invocation. Since you tagged c++11, you can use typename std::result_of<decltype(bar)*(int)>::type (of course, you need to #include <type_traits>).
For the benefit of future readers: I would like to address this question in the perspective of c++17. result_of is expects template argument of the form F(Args...), which suffers since the function type is the return type and is extremely limited. In c++17, invoke_result is introduced, and is way better than result_of: std::invoke_result_t<decltype(bar), int>. Very intuitive.
As a part of a much larger project, one of my objects (Thing in MWE) has a set of filters (filterStrong, filterWeak) defined on it. The goal is to use all the implemented filters in complexFilteringProcedure, where the user could chose the filtering rule through a parameter, and the function itself would depend on the success of the filtering rule chosen. The function complexFilteringProcedure would act on an object of type Thing, and call one of its private methods (filtering rules) depending on the parameter.
I implemented this by holding a vector of all possible filters in filteringOptions and implementing a single public filtering interface, filterUsingRule. Ideally, this would allow me to later on add new filtering rules to the project as I need them, and only modify the setFilteringFunction where the filter list is initialized.
Now, I started writing a new set of filtering rules, and realized all of them could be obtained by decorating the current filtering rules all in the same manner (softenFilter; please do correct me if "decorating" is the wrong expression here). I remembered reading into std::bind recently and taught, great. I would also like to add all the decorated filtering rules in my list of filteringOptions, that is, every original filter decorated with softenFilter.
Reading up a little bit more on std::bind, I think the possible reasons for my problems are twofold:
the return type of std::bind is a templated mess, and definitely not Thing::filteringFunction
I might be binding the this referring to the calling object when defining softStrong and softWeak
But, I am stuck further than that, not sure how to look for a solution to my specific problem. My main question are: Can this functionality be achieved? (functionality of filterUsingRule) and further, Can this functionality be achieved elegantly? (I know I could always define a set of functions bool softStrong(int param) { return softenFilter(filterStrong, param); } that manually bind the filters to the decorator, but I was hoping that std::bind or some new C++ magic would help with that).
The MWE recreating what I have successfully done and what I would like to achieve is as follows:
#include <iostream>
#include <vector>
#include <functional>
class Thing{
private:
int basicFilter;
typedef bool (Thing::*filteringFunction)(int);
static std::vector<filteringFunction> filteringOptions;
bool filterStrong(int parameter) {return parameter > basicFilter*2;}
bool filterWeak(int parameter) {return parameter > basicFilter;}
bool softenFilter(filteringFunction f, int parameter){
if (!((this->*(f))(parameter)))
return (this->*(f))(parameter+2);
return true;
}
void setFilteringFunctions(void){
Thing::filteringOptions.emplace_back(&Thing::filterStrong);
Thing::filteringOptions.emplace_back(&Thing::filterWeak);
auto softStrong = std::bind(&Thing::softenFilter,
&Thing::filterStrong,
std::placeholders::_1); // ok
auto softWeak = std::bind(&Thing::softenFilter,
&Thing::filterWeak,
std::placeholders::_1); // ok
filteringOptions.emplace_back(&softStrong); // how?
filteringOptions.emplace_back(softWeak); // how?
}
public:
Thing(int basicFilter) : basicFilter(basicFilter){
if (Thing::filteringOptions.empty())
setFilteringFunctions();
}
bool filterUsingRule(int parameter, int rule = 0){
return ((int)Thing::filteringOptions.size() > rule) &&
(this->*(Thing::filteringOptions[rule]))(parameter);
}
};
std::vector <Thing::filteringFunction> Thing::filteringOptions(0);
void complexFilteringProcedure(Thing &aThing, int parameter, int rule){
// do a lot of things
if (aThing.filterUsingRule(parameter, rule))
std::cout << "Filtering with " << rule << "successful" << std::endl;
else
std::cout << "Filtering with " << rule << "failed" << std::endl;
// and some more things
}
int main(void){
Thing myThing(5), otherThing(10);
complexFilteringProcedure(myThing, 7, 0); // uses strong rule
complexFilteringProcedure(otherThing, 7, 1); // uses weak rule
complexFilteringProcedure(myThing, 7, 2); // how to do this correctly?
complexFilteringProcedure(otherThing, 7, 3); // or this?
}
You might use std::function
using filteringFunction = std::function<bool (Thing&, int)>;
and then
void setFilteringFunctions()
{
Thing::filteringOptions.emplace_back(&Thing::filterStrong);
Thing::filteringOptions.emplace_back(&Thing::filterWeak);
auto softStrong = std::bind(&Thing::softenFilter,
std::placeholders::_1,
&Thing::filterStrong,
std::placeholders::_2
);
auto softWeak = std::bind(&Thing::softenFilter,
std::placeholders::_1,
&Thing::filterWeak,
std::placeholders::_2);
Thing::filteringOptions.emplace_back(&softStrong);
Thing::filteringOptions.emplace_back(&softWeak);
// or
Thing::filteringOptions.emplace_back([](Thing& instance, int param){
return instance.filterStrong(param + 2) });
}
You'll have to use a specialization of std::function as your vector element type. The key issue is that the object returned by std::bind() is not a bare function pointer. It is rather a Callable -- a function object -- it is some type (exactly what type is unimportant and in fact unspecified) that has an operator() with the appropriate return type which takes the appropriate parameters. This is exactly the role of std::function -- a type which can wrap any Callable of the correct signature in a way that lets you handle it with a known concrete type regardless of the actual type of the Callable.
typedef std::function<bool(int)> filteringFunction;
static std::vector<filteringFunction> filteringOptions;
// now can you store your member function pointers in
// filteringOptions after bind()ing the first parameter
// as you've already done
To satisfy the skeptics, here is the OP's code modified to use this technique.
#include <iostream>
#include <vector>
#include <functional>
class Thing{
private:
int basicFilter;
typedef std::function<bool(int)> filteringFunction;
static std::vector<filteringFunction> filteringOptions;
bool filterStrong(int parameter) {return parameter > basicFilter*2;}
bool filterWeak(int parameter) {return parameter > basicFilter;}
bool softenFilter(filteringFunction f, int parameter){
if (!f(parameter))
return f(parameter + 2);
return true;
}
void setFilteringFunctions(void){
filteringFunction strong = std::bind(&Thing::filterStrong,
this, std::placeholders::_1);
filteringFunction weak = std::bind(&Thing::filterWeak,
this, std::placeholders::_1);
filteringFunction softStrong = std::bind(&Thing::softenFilter,
this, strong, std::placeholders::_1);
filteringFunction softWeak = std::bind(&Thing::softenFilter,
this, weak, std::placeholders::_1);
filteringOptions.emplace_back(softStrong);
filteringOptions.emplace_back(softWeak);
}
public:
Thing(int basicFilter) : basicFilter(basicFilter){
if (Thing::filteringOptions.empty())
setFilteringFunctions();
}
bool filterUsingRule(int parameter, int rule = 0){
return ((int)Thing::filteringOptions.size() > rule) &&
filteringOptions[rule](parameter);
}
};
std::vector <Thing::filteringFunction> Thing::filteringOptions(0);
void complexFilteringProcedure(Thing &aThing, int parameter, int rule){
// do a lot of things
std::cout << "Filtering: " << aThing.filterUsingRule(parameter, rule) << std::endl;
// and some more things
}
int main(void){
Thing myThing(5), otherThing(10);
complexFilteringProcedure(myThing, 7, 0); // uses strong rule
complexFilteringProcedure(otherThing, 7, 1); // uses weak rule
//complexFilteringProcedure(myThing, 7, 2); // how to use soft strong rule?
//complexFilteringProcedure(otherThing, 7, 3); // how to use soft weak rule?
}
typedef std::function<bool(Thing*, int)> filteringFuction;
Now you can use static functions as well as std::bind and lambda or any callable that accepts an int and returns bool.
static bool test(Thing*, int);
static bool decoratee(Thing*, bool , int);
this->filteringOptions.emplace_back([](Thing* sth, int x){return false;});
this->filteringOptions.emplace_back(&Thing::weakFilter);
this->filteringOptions.emplace_back(std::bind(decoratee, _1, false, _2));
this->filteringOptions.emplace_back(&test);
int param;
for(auto& callee:this->filteringOptions)
callee(this,param);
Is it possible, with C++ 11 or Boost, to create an object that stores an object pointer (instance), method pointer and some arguments and can invoke this method with these arguments later? I mean - how to do it using only std or Boost templates? I'm pretty sure it is possible, but don't know what's the best way.
And here's the real question: is it in any way possible to store several such objects that refer to different methods (with diferent signatures) in the same container?
That's the classic use case for std::bind and std::function:
#include <functional>
#include <vector>
using namespace std::placeholders; // for _1, _2, ...
std::vector<std::function<int(double, char)>> v;
Foo x;
Bar y;
v.emplace_back(std::bind(&Foo::f, &x, _1, _2)); // int Foo::f(double, char)
v.emplace_back(std::bind(&Bar::g, &y, _2, true, _1)); // int Bar::g(char, bool, double)
v.emplace_bacK(some_free_function); // int some_free_function(double, char)
To use:
for (auto & f : v) { sum += f(1.5, 'a'); }
Check out std::bind offered by C++11. It does exactly what you want. You don't even need boost for this. For example:
class C
{
public:
void Foo(int i);
}
C c;
// Create a function object to represent c.Foo(5)
std::function<void(void)> callLater=std::bind(&C::Foo,std::ref(c),5);
// Then later when you want to call c.Foo(5), you do:
callLater();
I would expect the following example Boost Phoenix expression to compile.
What am I missing?
int plus(int a,int b)
{
return a+b;
}
void main(int argc,char** argc)
{
auto plus_1 = phx::bind(&plus,1,arg1);
auto value = phx::lambda[phx::val(plus_1)(arg1)]()(1);
std::cout << value << std::endl;
}
auto plus_1 = phx::bind(&plus,1,arg1);
After this line, plus_1 is a function object that takes one int argument and adds one to it.
phx::lambda[plus_1(arg1)](1);
Whoops. This isn't going to work because (as we said above) plus_1 is a function object that takes one int argument and adds one to it. Here, you're trying to invoke it with arg1.
It isn't obvious from your code what you expect it to do. Can you clarify?
====EDIT====
I see you've edited the code in your question. Your code is still wrong but for a different reason now. This:
phx::val(plus_1)(arg1)
... uses val to create a nullary function that returns the plus_1 unary function. You then try to invoke the nullary function with arg1. Boom.
Here is code that executes and does (what I believe) you intend:
#include <iostream>
#include <boost/phoenix/phoenix.hpp>
namespace phx = boost::phoenix;
using phx::arg_names::arg1;
int plus(int a,int b)
{
return a+b;
}
int main()
{
auto plus_1 = phx::bind(&plus, 1, arg1);
int value = phx::bind(phx::lambda[plus_1], arg1)(1);
std::cout << value << std::endl;
}
The first bind takes the binary plus and turns it into a unary function with the first argument bound to 1. The second bind creates a new unary function that is equivalent to the first, but it does so by safely wrapping the first function using lambda. Why is that necessary? Consider the code below, which is equivalent, but without the lambda:
// Oops, wrong:
int value = phx::bind(phx::bind(&plus, 1, arg1), arg1)(1);
Notice that arg1 appears twice. All expressions get evaluated from the inside out. First, we'll bind the inner arg1 to 1, then evaluate the inner bind yielding 2, which we then try to bind and invoke. That's not going to work because 2 isn't callable.
The use of lambda creates a scope for the inner arg1 so it isn't eagerly substituted. But like I said, the use of the second bind, which forces the need for lambda, yields a function that is equivalent to the first. So it's needlessly complicated. But maybe it helped you understand about bind, lambda and Phoenix scopes.
It's not clear to me what you're trying to accomplish by using lambda here, but if you just want to call plus_1 with 1 (resulting in 2), it's much simpler than your attempt:
#include <iostream>
#include <boost/phoenix.hpp>
int plus(int a, int b)
{
return a + b;
}
int main()
{
namespace phx = boost::phoenix;
auto plus_1 = phx::bind(plus, 1, phx::arg_names::arg1);
std::cout << plus_1(1) << '\n';
}
Online demo
If this isn't what you're trying to accomplish, then you need to describe what you actually want. :-]
Perhaps this can explain it better.
Phoenix is not magic; it is first and foremost C++. It therefore follows the rules of C++.
phx::bind is a function that returns a function object, an object which has an overloaded operator() that calls the function that was bound. Your first statement stores this object into plus_1.
Given all of this, anytime you have the expression plus_1(...), this is a function call. That's what it is; you are saying that you want to call the overloaded operator() function on the type of that object, and that you are going to pass some values to that function.
It doesn't matter whether that expression is in the middle of a [] or not. phx::lambda cannot make C++ change its rules. It can't make plus_1(...) anything other than an immediate function call. Nor can arg1 make plus_1(...) not an immediate function call.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
std::bind a bound function
void foo0(int val) { std::cout << "val " << val << "\n"; }
void foo1(int val, std::function<void (int)> ftor) { ftor(val); }
void foo2(int val, std::function<void (int)> ftor) { ftor(val); }
int main(int argc, char* argv[]) {
auto applyWithFoo0 ( std::bind(foo0, std::placeholders::_1) );
//std::function<void (int)> applyWithFoo0 ( std::bind(foo0, std::placeholders::_1) ); // use this instead to make compile
auto applyFoo1 ( std::bind(foo1, std::placeholders::_1, applyWithFoo0) );
foo2(123, applyFoo1);
}
The sample above does not compile giving multiple errors like: Error 1 error C2780: '_Ret std::tr1::_Callable_fun<_Ty,_Indirect>::_ApplyX(_Arg0 &&,_Arg1 &&,_Arg2 &&,_Arg3 &&,_Arg4 &&,_Arg5 &&,_Arg6 &&,_Arg7 &&,_Arg8 &&,_Arg9 &&) const' : expects 10 arguments - 2 provided.
Using the commented line with explicit type does compile. It seems that the type inferred by auto is not correct. What is the problem with auto in this case?
Platform: MSVC 10 SP 1, GCC 4.6.1
The issue is that std::bind treats "bind expression" (like your applyWithFoo0) differently from other types. Instead of calling foo1 with applyWithFoo0 as parameter it tries to invoke applyWithFoo0 and pass its return value to foo1. But applyWithFoo0 doesn't return anything that is convertible to std::function<void(int)>. The intention of handling "bind expressions" like this is to make them easily composable. In most cases you probably don't want bind expression to be passed as function parameters but only their results. If you explicitly wrap the bind expression into a function<> object, the function<> object will simply be passed to foo1 directly since it is not a "bind expression" and therefore not handled specially by std::bind.
Consider the following example:
#include <iostream>
#include <functional>
int twice(int x) { return x*2; }
int main()
{
using namespace std;
using namespace std::placeholders;
auto mul_by_2 = bind(twice,_1);
auto mul_by_4 = bind(twice,mul_by_2); // #2
auto mul_by_8 = bind(twice,mul_by_4); // #3
cout << mul_by_8(1) << endl;
}
This actually compiles and works because instead of passing a functor to twice like you might expect from the bind expressions #2 and #3, bind actually evaluates the passed bind expressions and uses its result as function parameter for twice. Here, it is intentional. But in your case, you tripped over this behaviour by accident because you actually want bind to pass the functor itself to the function instead of its evaluated value. Wrapping the functor into a function<> object is obviously a work-around for that.
In my opinion this design decision is a bit awkward because it introduces an irregularity people have to know about to be able to use bind correctly. Maybe, we'll get another more satisfying work around in the future like
auto applyFoo1 = bind( foo1, _1, noeval(applyWithFoo0) );
where noeval tells bind not to evaluate the expression but to pass it directoy to the function. But maybe the other way around -- explicitly telling bind to pass the result of a functor to the function -- would have been a better design:
auto mul_by_8 = bind( twice, eval(mul_by_4) );
But I guess, now it's too late for that ...
My guess is the parentheses around the std::bind make the parser think you're declaring functions named applyWithFoo0 and applyFoo1.
std::bind returns a functor the type of which auto should be able to detect.
Try this:
int main(int argc, char* argv[]) {
auto applyWithFoo0 = std::bind(foo0, std::placeholders::_1);
//std::function<void (int)> applyWithFoo0 std::bind(foo0, std::placeholders::_1) ); // use this instead to make compile
auto applyFoo1 = std::bind(foo1, std::placeholders::_1, applyWithFoo0);
foo2(123, applyFoo1);
}