I have the below code where I am trying to print the multiplication table of 10.
I have bound the function multiply with two parameters 5 and 2. I am passing the bound function to create a lambda. And then I am attempting to pass the lambda to the for_each loop to print the Multiplication Table. I intutively kind of know that I may be pushing it a bit too far. But I don't know the exact reason. Can someone explain.
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
#include <functional>
#include <future>
#include <array>
#include <unistd.h>
using namespace std;
using namespace std::placeholders;
int multiply(int a, int b, int c)
{
return a*b*c;
}
int main()
{
auto f = std::bind(multiply, 5, 2, _1);
std::function<int(int,int,int)> f1 = [f](int a){cout << "Multiplication Table (10) :" << f(a) << endl; };
vector<int> vec = {1,2,3,4,5,6,7,8,9,10};
for_each(vec.begin(), vec.end(), f1);
return 0;
}
The error that I am getting is shown below.
/home/karthik/Workspace/cpppen/learning/main.cpp: In function ‘int main()’:
/home/karthik/Workspace/cpppen/learning/main.cpp:26:107: error: conversion from ‘main()::<lambda(int)>’ to non-scalar type ‘std::function<int(int, int, int)>’ requested
std::function<int(int,int,int)> f1 = [f](int a){cout << "Multiplication Table (10) :" << f(a) << endl;};
^
In file included from /usr/include/c++/7/algorithm:62:0,
from /home/karthik/Workspace/cpppen/learning/main.cpp:6:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Funct = std::function<int(int, int, int)>]’:
/home/karthik/Workspace/cpppen/learning/main.cpp:30:40: required from here
/usr/include/c++/7/bits/stl_algo.h:3884:5: error: no match for call to ‘(std::function<int(int, int, int)>) (int&)’
__f(*__first);
~~~^~~~~~~~~~
In file included from /usr/include/c++/7/functional:58:0,
from /home/karthik/Workspace/cpppen/learning/main.cpp:8:
/usr/include/c++/7/bits/std_function.h:701:5: note: candidate: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int, int, int}]
function<_Res(_ArgTypes...)>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am passing a function with two bound and one placeholder parameter.
No you're not. Your lambda looks like this:
[f](int a) {
cout << "Multiplication Table (10) :" << f(a) << endl;
}
That is the callable object that you are passing to std::function. Now, the lambda is callable with only one argument, not three. Notice:
[/*...*/](int a){ /*...*/ }
// ^^^^^
// one parameter
Likewise, f is a callable object with only one parameter. You can't call it with three arguments, because you've bound two parameters to specific values, so, for all intents and purposes, there are no three parameters. Maybe this makes it more clear:
auto add = [](int a, int b) { return a + b; };
auto addTo5 = [&add](int a) { return add(a, 5); };
add(1, 2); // ok, lambda takes two parameters
addTo5(3); // ok, lambda takes one parameter
addTo5(1, 2); // not ok, lambda doesn't take two parameters
std::function<int(int, int)> fadd = add; // ok
std::function<int(int)> faddTo5 = addTo5; // ok
std::function<int(int, int)> faddTo5fail = addTo5; // not ok, same reason
// addTo5 is approximately what std::bind does: It generates an object which has
// several variables "fixed", and so only takes the placeholder arguments that aren't
// specified.
So, the fix is to change f1's type to reflect what you are actually storing; a callable that takes an int and returns nothing:
std::function<void(int)> f1 = /*...*/;
// ^^^^
// lambda returns nothing
Related
I wonder why std::function<...(...)> & needs to be specified as const when passed as an input parameter to a function. AFAIK there is no way to change it, right? Here is an example that compiles and runs fine. If I remove the const qualifier I get an error:
#include <iostream>
#include <functional>
//int foo(std::function<int(int)> &f)
int foo(const std::function<int(int)> &f)
{
return f(6);
}
int main(int argc, char **argv)
{
auto f1 = [=](int i){ if (i<5) {return 8*2;} else {return 2;} };
auto f2 = [=](int i){ if (i>3) {return i*i;} else {return 2;} };
std::cout << foo(f1) << "\n";
}
When I use the declaration without the const I get the following error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:13:21: error: cannot bind non-const lvalue reference of type ‘std::function<int(int)>&’ to an rvalue of type ‘std::function<int(int)>’
std::cout << foo(f1) << "\n";
^
In file included from /usr/include/c++/7/functional:58:0,
from main.cpp:2:
/usr/include/c++/7/bits/std_function.h:685:7: note: after user-defined conversion: std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = main(int, char**)::<lambda(int)>; <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = int; _ArgTypes = {int}]
function<_Res(_ArgTypes...)>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:4:5: note: initializing argument 1 of ‘int foo(std::function<int(int)>&)’
int foo(std::function<int(int)> &f)
^~~
A lambda is not a std::function, but a std::function can be created from a lambda. When you pass either f1 or f2 to foo(), the compiler must construct a temporary std::function object to give to the f parameter. However, an lvalue reference to a non-const object cannot be bound to a temporary object, exactly as the error message says.
To allow the passing of a temporary object, the f parameter must be changed to take the std::function by either:
value
lvalue reference to a const object
rvalue reference
Otherwise, you have to construct the std::function yourself in a variable, then pass that instead:
#include <iostream>
#include <functional>
//int foo(const std::function<int(int)> &f)
int foo(std::function<int(int)> &f)
{
return f(6);
}
int main(int argc, char **argv)
{
auto f1 = [=](int i){ if (i<5) {return 8*2;} else {return 2;} };
auto f2 = [=](int i){ if (i>3) {return i*i;} else {return 2;} };
std::function<int(int)> f = f1;
std::cout << foo(f) << "\n";
}
I'm trying to understand how std::bind and std::function work.
I cannot get the following code to compile:
#include <iostream>
#include <string>
#include <functional>
void function(int a, float b, std::string const &s)
{
std::cout << "printing function" << std::endl;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << s << std::endl;
}
int main(int argc, char *argv[])
{
std::bind(&function, 10, 11.1, "hello")();
std::function<void(int, float, std::string const&)> fun = std::bind(&function, 10, std::placeholders::_1, std::placeholders::_2);
fun(0.2, "world");
return 0;
}
the compiler complains that:
main.cpp: In function 'int main(int, char**)':
main.cpp:16:69: error: conversion from 'std::_Bind_helper<false, void (*)(int, float, const std::__cxx11::basic_string<char>&), int, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind<void (*(int, std::_Placeholder<1>, std::_Placeholder<2>))(int, float, const std::__cxx11::basic_string<char>&)>}' to non-scalar type 'std::function<void(int, float, const std::__cxx11::basic_string<char>&)>' requested
std::function<void(int, float, std::string const&)> fun = std::bind(&function, 10, std::placeholders::_1, std::placeholders::_2);
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
please, can someone explain? and how do I fix this error?
You are almost there, just change the type of fun to
std::function<void(float, std::string const&)> fun = std::bind(...);
// ^^ no more int here
fun(0.2, "world");
// ^^^^^^^^^^^^ those types must match the above signature
Note that you change the function signature when fixing the first function argument of type int to the value 10. Hence, it can't be in the type of the std::function instantiation.
Further note that Scott Meyers suggests in Item 34 of Effective Modern C++ to replace the std::bind usage with a lambda, e.g.
auto fun = [](float b, std::string const& s){ function(10, b, s); };
// Identical invocation:
fun(0.2, "world");
I have been reading up on, how to perform a std::bind on a regular function.
And store the free function or member function into a std::function.
However, if I try to use a placeholder for one argument and an actual value for the other argument; I am not able to make a call(causes compilation error) to the std::function
So I tried the following code:
#include <random>
#include <iostream>
#include <memory>
#include <functional>
int g(int n1, int n2)
{
return n1+n2;
}
int main()
{
using namespace std::placeholders; // for _1, _2, _3...
std::function<int(int,int)> f3 = std::bind(&g, std::placeholders::_1, 4);
std::cout << f3(1) << '\n';
//this works just fine
auto f4 = std::bind(&g, std::placeholders::_1, 4);
std::cout << f4(1) << '\n';
}
I get the following error g++ 4.7
prog.cpp: In function 'int main()':
prog.cpp:17:22: error: no match for call to '(std::function<int(int, int)>) (int)'
std::cout << f3(1) << '\n';
^
In file included from /usr/include/c++/4.9/memory:79:0,
from prog.cpp:3:
/usr/include/c++/4.9/functional:2142:11: note: candidate is:
class function<_Res(_ArgTypes...)>
^
/usr/include/c++/4.9/functional:2434:5: note: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int, int}]
function<_Res(_ArgTypes...)>::
^
/usr/include/c++/4.9/functional:2434:5: note: candidate expects 2 arguments, 1 provided
If you're binding an argument to the function int g(int, int), what remains as a callable is a function taking one int as an argument, not two.
Try this:
std::function<int(int)> f3 = std::bind(&g, std::placeholders::_1, 4);
the type of your std::function should be:
std::function<int(int)> f3 = std::bind(&g, std::placeholders::_1, 4);
~~~
one argument
Your bind creates a function with one parameter. That's why you call f3 as this:
std::cout << f3(1) << '\n';
note: candidate expects 2 arguments, 1 provided
should have been your clue
Code like this:
#include <iostream>
#include <future>
#include <thread>
#include <unistd.h>
int foo(int n) {
n = n*1000;
sleep(1);
return n;
}
int main(void) {
std::packaged_task<int (int)> task(std::bind(foo, 3));
std::future<int> f(task.get_future());
std::thread trd(std::move(task));
std::cout << f.get() << std::endl;
return 0;
}
gcc report:
In file included from /usr/include/c++/4.8.2/future:38:0,
from a.cpp:2:
/usr/include/c++/4.8.2/functional: In instantiation of ‘struct std::_Bind_simple<std::packaged_task<int(int)>()>’:
/usr/include/c++/4.8.2/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = std::packaged_task<int(int)>; _Args = {}]’
a.cpp:16:33: required from here
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::packaged_task<int(int)>()>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8.2/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::packaged_task<int(int)>()>’
_M_invoke(_Index_tuple<_Indices...>)
^
make: *** [a] Error 1
My gcc version is 4.8.2 on fedora 20
The function foo is declared as:
int foo(int);
It has the function type int(int) (taking a parameter int and returning int).
However, the resulting callable returned by std::bind when you bind 3 to the first parameter has a different function type: int(), e.g.:
auto func = std::bind(foo, 3) // Bind 3 to the first parameter.
func(); // Calling func requires no parameter.
Solution
The template parameter specified when declaring the std::packaged_task should be specified as int(), e.g.:
std::packaged_task<int()> task{std::bind(foo, 3)};
Alternatively don't bind the parameter to 3 when constructing the std::packaged_task, instead supply it directly when creating the std::thread object:
std::packaged_task<int(int)> task{foo}; // Don't bind 3
auto f = task.get_future();
std::thread trd{std::move(task), 3}; // Supply 3 here instead.
std::cout << f.get() << std::endl;
Furthermore
Make sure to call trd.join() before returning from function main.
When using std::thread also use the sleep function from the Standard Library instead of the non-portable sleep, e.g.:
std::this_thread::sleep_for(std::chrono::seconds(1));
Also when using std::move you should include the header <utility> in case the other headers don't include it.
I'm trying somme features of c++0x (under gcc 4.5):
I know it's possible to convert a std::function<void(string, string)> to std::function<void()> when arguments are specified at compilation; but is it possible when arguments are submitted at runtime ?
#include <iostream>
#include <utility>
#include <string>
using namespace std;
using namespace placeholders;
class Print{
public:
void print1(string s1, string s2){ cout<<"s1 : "<<s1<<" s2 : "<<s2<<endl;}
void print2(string s1){ cout<<"s1 : "<<s1<<endl;}
};
Print p = Print();
function<void(string, string)> f1(bind(&Print::print1, &p, _1, _2));
function<void()> f = f1;
I get those errors :
/usr/include/c++/4.5/functional:2103:6: instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::function<void(std::basic_string<char>, std::basic_string<char>)>, _Res = void, _ArgTypes = {}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void()>::_Useless]’
../src/Cpp0x_test.cpp:345:34: instantiated from here
/usr/include/c++/4.5/functional:1713:9: error: no match for call to ‘(std::function<void(std::basic_string<char>, std::basic_string<char>)>) ()’
/usr/include/c++/4.5/functional:2111:5: note: candidate is: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void, _ArgTypes = {std::basic_string<char>, std::basic_string<char>}]
Actually I need to do :
function<void(string, string)> f1(bind(&Print::print1, &p, _1, _2));
function<void(string)> f2(bind(&Print::print2, &p, _1));
function<void()> fx1 = f1;
function<void()> fx2 = f2;
std::vector<function<void()> > vec;
vec.push_back(fx1);
vec.push_back(fx2);
//then, later
function<void()> call1 = vec[0];
function<void()> call2 = vec[1];
call1("test1", "test2");
call2("test3");
The question doesn't really make sense.
I know it's possible to convert a std::function<void(string, string)>
to std::function<void()> when arguments are specified at compilation;
but is it possible when arguments are submitted at runtime ?
If you're talking about doing this to set the arguments "at compilation":
string arg1,arg2;
function<void()> f = bind(f1,arg1,arg2); // f = [=] { f1(arg1,arg2); };
this is actually doing the binding at runtime. Whatever value those arguments have when bind is called, even if they are set at runtime, say, from user input, the calling f() will use those runtime values.
Perhaps you mean that the above code binds f1 to the value of arg1 and arg2 at the time bind is called, and that changing the values of the objects used in bind later on doesn't affect the values used in calls to f(). There's a way around that:
string arg1,arg2;
function<void()> f =
bind(f1,std::ref(arg1),std::ref(arg2)); // f = [&,f1] { f1(arg1,arg2); };
This causes f to hold references to the objects instead of just the static value used at the time bind is called. You can now assign new values to arg1 and arg2 and when f() is called the new values will be used. Note that you have to make sure that the references held by f remain valid and don't become dangling references so long as f can still be called.
function<void(string)> foo = [](string s){ cout << s; };
string arg = "Hello,";
function<void()> bar = bind(foo,ref(arg)); // bar = [=,&arg] { foo(arg); };
bar(); // prints "Hello,"
arg = " World!"
bar(); // prints " World!"
Its possible using bind aswell:
string arg1, arg2;
function<void()> f(bind(f1, arg1, arg2));
f(); // calls f1(arg1, arg2) with their values at the time of bind
Let's see if I understand your requirement.
Why don't you just store the arguments in a vector instead of the functions?
std::vector<std::tuple<std::string,std::string>> v;
v.push_back(std::make_tuple("a", "b")); // repeat
// Later that day...
for(auto& t : v) {
f(get<0>(t), get<1>(t));
}