stdbind

c++: How to write a std::bind-like object that checks for superfulous parameters?

痞子三分冷 提交于 2019-12-25 07:49:52
问题 According to http://en.cppreference.com/w/cpp/utility/functional/bind, for std::bind Member function operator() ... If some of the arguments that are supplied in the call to g() are not matched by any placeholders stored in g, the unused arguments are evaluated and discarded. Quoting the examples, one can do: void f(int n1, int n2, int n3, const int& n4, int n5) { std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n'; } int main() { auto f1 = std::bind(f, _2, _1, 42, std

Why are placeholders required in std::bind in this case?

北慕城南 提交于 2019-12-24 04:44:09
问题 While answering this question, I see the following fact by accident. Please see this example: void func1(const char *str1, const char *str2) { puts(str1); puts(str2); } ... auto fn = std::bind(func1, "asdf"); fn("1234"); It is failed to compile it: prog.cpp: In function ‘int main()’: prog.cpp:11:14: error: no match for call to ‘(std::_Bind<void (*(const char*))(const char*, const char*)>) (const char [5])’ fn("1234"); ^ If I change code into this, it works well: auto fn = std::bind(func1,

Why should bind() be deprecated?

十年热恋 提交于 2019-12-23 12:33:12
问题 Reading the proposal for C++17 about removing some deprecated, old and unused parts of the standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm) i find section D.9 a bit strange: D.9 "Binders" [depr.lib.binders] This defines bind1st()/bind2nd(), which were strictly superseded by bind(). (In the future, I'll argue that bind() itself has been superseded by lambdas and especially generic lambdas, so bind() should be deprecated, but that isn't part of this proposal.) What I

Understanding std::function and std::bind

非 Y 不嫁゛ 提交于 2019-12-21 04:02:52
问题 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

Partial Binding of Function Arguments

…衆ロ難τιáo~ 提交于 2019-12-20 18:39:23
问题 Is there a way to partially bind the first/last n arguments of a callable object (e.g. function) without explicitly specifying the rest of the arguments? std::bind() seems to require that all the arguments are be bound, those that are to be left should be bound to std::placeholders::_1 , _2 , _3 etc. Is it possible to write a bind_first() / bind_last() for partial binding starting from the first/last argument and that automagically inserts the placeholders for any remaining unbound arguments

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

帅比萌擦擦* 提交于 2019-12-20 02:43:16
问题 This question already has answers here : std::bind lose reference when delivered as rvalue reference (2 answers) Closed 4 years ago . 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? 回答1

Different overloads with std::function parameters is ambiguous with bind (sometimes)

别说谁变了你拦得住时间么 提交于 2019-12-19 04:01:08
问题 I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call

Short way to std::bind member function to object instance, without binding parameters

梦想与她 提交于 2019-12-18 19:16:11
问题 I have a member function with several arguments. I'd like to bind it to a specific object instance and pass this to another function. I can do it with placeholders: // actualInstance is a MyClass* auto callback = bind(&MyClass::myFunction, actualInstance, _1, _2, _3); But this is a bit clumsy - for one, when the number of parameters changes, I have to change all the bind calls as well. But in addition, it's quite tedious to type all the placeholders, when all I really want is to conveniently

Executing bound std::function throws std::bad_function_call

牧云@^-^@ 提交于 2019-12-13 09:01:20
问题 I want to bind a member function to a std::function<void(void)> . I heard that member functions take one extra parameter which is the instance pointer. Therefore I call std::bind(&Class::Function, this, parameter) but when I execute the function object, it throws a runtime error. Unhandled exception at at 0x748D4B32 in Application.exe: Microsoft C++ exception: std::bad_function_call at memory location 0x0114F4E8. The parameter is a pointer to one of my own struct s. How am I doing wrong? What

How to std::bind a smart pointer return method?

余生颓废 提交于 2019-12-13 01:49:19
问题 So I have this method inside my Bar class: std::shared_ptr<sf::Sprite> Bar::getStuff() const { //... } And I have my callback typedef: typedef std::function<void()> Callback; void Foo::registerCallback(const Callback& callback) { //... } And now I want to use std::bind on this method, like: Foo foo; Bar bar; //I create an instance of an Bar, called bar. foo.registerCallback(std::bind(&Bar::getStuff, std::ref(bar))); //<--- ERROR!!!! ERROR: error C2562: 'std::_Callable_obj<std::_Bind<true,std: