stdbind

Should I be seeing significant differences between std::bind and boost::bind?

无人久伴 提交于 2019-12-05 08:08:55
I'm exploring the support for C++11 on the g++-4.7 (Ubuntu/Linaro 4.7.3-2ubuntu~12.04, to be specific) and I seem to be finding differences. In particular, if I comment out #include <boost/bind.hpp> and systematically replace occurrences of boost::bind with std::bind in the Boost ASIO async client example (taken from http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp ), the program no longer compiles. Any explanation for this? #include <functional> namespace boost { namespace asio { namespace stdplaceholders { static decltype ( :: std :: placeholders

How do I use bind to pass a member function as a function pointer?

こ雲淡風輕ζ 提交于 2019-12-05 00:39:03
问题 I'm trying to pass a member function as a function pointer so that I don't need to rely on singletons or global functions to handle Qt messages in Qt 5. As far as I can tell my std::function is of the correct type, it has the correct signature, and bind should be allowing me to jam in the implicit this pointer, essentially passing off a member function as a global/un-owned function. void ProgramMessageHandler::setAsMessageHandlerForProgram() { std::function<void(QtMsgType, const

C++11 random numbers and std::bind interact in unexpected way

▼魔方 西西 提交于 2019-12-04 23:02:39
问题 I am using GCC 4.6.3 and was trying to generate random numbers with the following code: #include <random> #include <functional> int main() { std::mt19937 rng_engine; printf("With bind\n"); for(int i = 0; i < 5; ++i) { std::uniform_real_distribution<double> dist(0.0, 1.0); auto rng = std::bind(dist, rng_engine); printf("%g\n", rng()); } printf("Without bind\n"); for(int i = 0; i < 5; ++i) { std::uniform_real_distribution<double> dist(0.0, 1.0); printf("%g\n", dist(rng_engine)); } return 0; } I

std::bind(): bind lambda with rvalue reference as argument

不问归期 提交于 2019-12-04 19:26:00
I am playing with std::bind and rvalue references, but I still don't figure out how it works, I have the following code: class Dog { public: Dog(const string &name) : name_(name) { cout << "Dog::ctor" << endl; } string GetName() { return name_; } private: string name_; }; auto bind_fun = bind([](Dog &&d){ cout << d.GetName() << endl; }, Dog("DogABC")); bind_fun(); When commenting out bind_fun() , or if the lambda takes Dog& rather than Dog&& , the code run fine with expected output. When bind_fun() is left uncommented, the following compile time error: test3.cpp:109:3: error: no matching

How do I store a vector of std::bind without a specific case for the template?

那年仲夏 提交于 2019-12-04 05:46:08
After going though a question on std::bind , I was wondering if it was possible to hold a vector of functions created by std::bind so I can avoid using std::function and its heavyweight wrapping. #include <iostream> #include <functional> #include <typeinfo> #include <vector> int add(int a, int b) {return a + b;} int main() { //I believe this here is just a special type of bound function. auto add2 = std::bind(add, std::placeholders::_1, 2); auto add3 = std::bind(add, std::placeholders::_1, 3); //Yup. std::cout << typeid(add2).name() << std::endl; //Here's the type of the second function std:

Does std::bind discard type information of parameters in C++11?

半世苍凉 提交于 2019-12-04 03:52:02
问题 Case where the problem occours Please consider the following c++ code: #include <functional> #include <iostream> #include <string> // Superclass class A { public: virtual std::string get() const { return "A"; } }; // Subclass class B : public A { public: virtual std::string get() const { return "B"; } }; // Simple function that prints the object type void print(const A &instance) { std::cout << "It's " << instance.get() << std::endl; } // Class that holds a reference to an instance of A class

std::bind and overloaded function

懵懂的女人 提交于 2019-12-04 02:16:16
Please refer the following code snippet. I want to use the std::bind for overloaded function foobar . It calls only the method with no arguments. #include <functional> #include <iostream> class Client { public : void foobar(){std::cout << "no argument" << std::endl;} void foobar(int){std::cout << "int argument" << std::endl;} void foobar(double){std::cout << "double argument" << std::endl;} }; int main() { Client cl; //! This works auto a1 = std::bind(static_cast<void(Client::*)(void)>(&Client::foobar),cl); a1(); //! This does not auto a2= [&](int) { std::bind(static_cast<void(Client::*)(int)>

C++11 random numbers and std::bind interact in unexpected way

丶灬走出姿态 提交于 2019-12-03 13:53:55
I am using GCC 4.6.3 and was trying to generate random numbers with the following code: #include <random> #include <functional> int main() { std::mt19937 rng_engine; printf("With bind\n"); for(int i = 0; i < 5; ++i) { std::uniform_real_distribution<double> dist(0.0, 1.0); auto rng = std::bind(dist, rng_engine); printf("%g\n", rng()); } printf("Without bind\n"); for(int i = 0; i < 5; ++i) { std::uniform_real_distribution<double> dist(0.0, 1.0); printf("%g\n", dist(rng_engine)); } return 0; } I expected both methods to generate a sequence of 5 random numbers. Instead, this is what I actually get

Understanding std::function and std::bind

限于喜欢 提交于 2019-12-03 12:25:57
I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better. For example: void fun() { } void hun(std::string) { } int main() { function<void(int)> g = &fun; //This fails as it should in my understanding. function<void(int)> f = std::bind(fun); //This works for reasons unknown to me function<void(int, std::string)> h = std::bind(hun); //this doesn't work return 0; } How is it possible to bind a function<void(int)> to a function that is void() . I could then call f(1) and get fun(). I would like to understand how this is

Why does bind not work with pass by reference? [duplicate]

对着背影说爱祢 提交于 2019-12-01 20:55:16
This question already has an answer here: std::bind lose reference when delivered as rvalue reference 2 answers I find pass by reference tends not to work when using std::bind. Here's an example. int test; void inc(int &i) { i++; } int main() { test = 0; auto i = bind(inc, test); i(); cout<<test<<endl; // Outputs 0, should be 1 inc(test); cout<<test<<endl; // Outputs 1 return 0; } Why isn't the variable incrementing when called via the function created with std bind? std::bind copies the argument provided, then it passes the copy to your function. In order to pass a reference to bind you need