std-function

std::function copying parameters?

不羁的心 提交于 2019-12-22 04:05:28
问题 My code: #include <iostream> #include <functional> using namespace std; struct A { A() = default; A(const A&) { cout << "copied A" << endl; } }; void foo(A a) {} int main(int argc, const char * argv[]) { std::function<void(A)> f = &foo; A a; f(a); return 0; } I'm seeing "copied A" twice on the console. Why is the object being copied twice, not once? How can I prevent this properly? 回答1: The specialization std::function<R(Args...)> has a call operator with the following declaration: R operator

Odd return behavior with std::function created from lambda (C++)

丶灬走出姿态 提交于 2019-12-22 01:54:03
问题 I'm having trouble with std::functions created from lambdas if the function returns a reference but the return type isn't explicitly called out as a reference. It seems that the std::function is created fine with no warnings, but upon calling it, a value is returned when a reference is expected, causing things to blow up. Here's a very contrived example: #include <iostream> #include <vector> #include <functional> int main(){ std::vector<int> v; v.push_back(123); std::function<const std:

Why std::function does not implicitly convert to bool in C++11? [duplicate]

喜夏-厌秋 提交于 2019-12-21 20:39:57
问题 This question already has an answer here : No viable conversion from std::function to bool (1 answer) Closed 3 years ago . Consider the following code. #include <functional> int main(void) { std::function<void()> f1; if (f1) { /* ok */ ... } bool b = f1; /* compile-error */ bool B = !f1; /* ok */ ... } std::function<> converts implicitly to bool in some circumstances but not in all of them. Assigning it to a bool -variable does not work, whereas the result of an operation or using it in an if

What is the difference between std::function and std::mem_fn

廉价感情. 提交于 2019-12-21 04:12:38
问题 I am having trouble figuring out the difference between the two function wrappers std::function and std::mem_fn. From the description, it seems to me that std::function does everything std::mem_fn does and more. In which instance would one use std::mem_fn over std::function ? 回答1: You can't really compare std::function with std::mem_fn . The former is a class template whose type you specify, and the latter is a function template with unspecified return type. There really isn't a situation in

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

binding member functions in a variadic fashion

a 夏天 提交于 2019-12-21 03:43:23
问题 I have a member function with a variable number of parameters, stored in a std::function , and I want to bind the instance and get an independent function object. template <class T, class R, class... Args> void connect(const T& t, std::function<R(const T&, Args...)> f) { std::function<R(Args...)> = /* bind the instance c into the function? */ } // ... Class c; connect(c, &Class::foo); For a fixed number of arguments I'd use std::bind , but I don't see how to do this for variadic parameters.

std::function as template parameter

天大地大妈咪最大 提交于 2019-12-20 19:31:33
问题 I currently have a map<int, std::wstring> , but for flexibility, I want to be able to assign a lambda expression, returning std::wstring as value in the map. So I created this template class: template <typename T> class ValueOrFunction { private: std::function<T()> m_func; public: ValueOrFunction() : m_func(std::function<T()>()) {} ValueOrFunction(std::function<T()> func) : m_func(func) {} T operator()() { return m_func(); } ValueOrFunction& operator= (const T& other) { m_func = [other]() ->

C++11 std::set lambda comparison function

蹲街弑〆低调 提交于 2019-12-20 08:23:56
问题 I want to create a std::set with a custom comparison function. I could define it as a class with operator() , but I wanted to enjoy the ability to define a lambda where it is used, so I decided to define the lambda function in the initialization list of the constructor of the class which has the std::set as a member. But I can't get the type of the lambda. Before I proceed, here's an example: class Foo { private: std::set<int, /*???*/> numbers; public: Foo () : numbers ([](int x, int y) {

How could I pass std::function as function pointer?

↘锁芯ラ 提交于 2019-12-19 10:21:17
问题 I am trying to write a class template and internally it use a C function (implementation of BFGS optimization, provided by the R environment) with the following interface: void vmmin(int n, double *x, double *Fmin, optimfn fn, optimgr gr, ... , void *ex, ... ); where fn and gr are function pointers of type typedef double optimfn(int n, double *par, void *ex); and typedef void optimgr(int n, double *par, double *gr, void *ex); respectively. My C++ class template looks like this: template

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