std-function

“No matching function for call to… unresolved overloaded function type”

拜拜、爱过 提交于 2019-12-11 16:02:01
问题 I am trying to create a library where the user can modify the behaviour of a function on an instance-level and still manage to access the members of this instance. This post is the continuation of this thread which is the continuation of this one. Praetorian suggested me to use std::function/bind to do that. Unfortunately, I have two errors: Pb #1: error: no matching function for call to ‘Child1::BindIt()’ Pb #2: error: no match for ‘operator=’ (operand types are ‘std::function’ and ‘std::

VS2013 std::function with member function [duplicate]

这一生的挚爱 提交于 2019-12-11 09:41:38
问题 This question already has an answer here : std::function not compiling in VS2012 (1 answer) Closed 5 years ago . I'm trying to use std::function with member functions like this: struct Foo { void bar(int) const { /* ... */ } }; //later on std::function<void(const Foo&, int)> fun = &Foo::bar; This works under GCC 4.8.1 but fails to compile under VS2013 with the following error: error C2664: 'void std::_Func_class<_Ret,const Foo &,int>::_Set(std::_Func_base<_Ret,const Foo &,int> *)' : cannot

std::function variable arguments in one vector/map

北战南征 提交于 2019-12-11 02:54:29
问题 how could one do this in c++ today without using two separate holders? typedef std::function<void(int a, int b)> f1; typedef std::function<void(int a)> f2; std::vector<f1> m; void add(f1 f) { m.push_back(f); } void add(f2 f) { // add one more (unused) parameter to f2 so we can add f2 to f1 vector holder? } can we somehow overload f1 function to include different set of parameters? could this be solved by variadic templates nowdays or something similar? 回答1: Create a new lambda matching the

C++ External function with pointer to function as parameter, used inside a class with a member function

时光总嘲笑我的痴心妄想 提交于 2019-12-10 17:42:13
问题 Fairly new to C++. Suppose I have a class: class A { private: double m_x, m_y; public: A(double x, double y): m_x {x} { m_y = extF(m_x, y, *intF); } double intF(double x) { return 2*x; } }; And it makes use of an external global function, defined elsewhere: double extF(double x, double y, std::function<double(double)> f) { if (x*y < 0) return f(x); else return f(y); } Formulas are bogus. This does not compile. I tried simple intF , A::*intF , &A::intF , even some unorthodox combinations, but

reinterpret_cast std::function* to and from void*

半腔热情 提交于 2019-12-10 14:16:40
问题 I'm suffering a segfault in a plugin when I call a std::function in it passed from the main executable, via converting it's address to/from void* . I can reproduce the problem in a few self-contained lines: #include <iostream> #include <functional> int main() { using func_t = std::function<const std::string& ()>; auto hn_getter = func_t{[]() { return "Hello"; }}; auto ptr = reinterpret_cast<void*>(&hn_getter); auto getter = reinterpret_cast<func_t*>(ptr); std::cout << (*getter)() << std::endl

Create a “do-nothing” `std::function` with any signature?

♀尐吖头ヾ 提交于 2019-12-10 14:16:20
问题 I would like to create a simple no-op std::function object with an arbitrary signature. To that end, I've created two functions: template <typename RESULT, typename... ArgsProto> std::function<RESULT(ArgsProto...)> GetFuncNoOp() { // The "default-initialize-and-return" lambda return [](ArgsProto...)->RESULT { return {}; }; } template <typename... ArgsProto> std::function<void(ArgsProto...)> GetFuncNoOp() { // The "do-nothing" lambda return [](ArgsProto...)->void {}; } Each of these works well

A const std::function wraps a non-const operator() / mutable lambda

你。 提交于 2019-12-10 13:49:41
问题 Consider the following example: #include <iostream> #include <functional> struct A { int i; void operator()() { std::cout << ++i; } }; void test(std::function<void()> const& fun) { fun(); } int main() { const std::function<void()> f{A{}}; test(f); test(f); } Here, a const std::function is able to call a non- const operator() . Output: 12 The same happens if I supply a mutable lambda e.g. test([x = 0]() mutable { ++x; }); How can that be? Is it normal that a const std::function may wrap a

Create a std::function type with limited arguments

半腔热情 提交于 2019-12-09 12:31:28
问题 Given the type of a callable function C , I want to get at compile time a std::function ; the type of which: has the same return type of function C the argument types are the first N argument types of function C This means that, for a given type void(int, char, double) and a given N , the type of the function is: N = 1 => result type: std::function<void(int)> N = 2 => result type: std::function<void(int, char)> N = 3 => result type: std::function<void(int, char, double)> N > 3 => compile time

C++11 std::function and perfect forwarding

梦想与她 提交于 2019-12-09 08:28:42
问题 Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not R operator()(ArgTypes&&...) const; ? One would think that to correctly forward parameters, we need the && and then use std::forward<ArgTypes>... in the function body when forwarding the call? I partially reimplemented std::function to test this and I found out that if I use the &&, I get "cannot bind 'xxx' lvalue to 'xxx&&'" from g++ when I try later to pass parameters by value to

How do I `std::bind` a non-static class member to a Win32 callback function `WNDPROC`?

十年热恋 提交于 2019-12-08 17:51:41
问题 I'm trying to bind a non-static class member to a standard WNDPROC function. I know I can simply do this by making the class member static. But, as a C++11 STL learner, I'm very interested in doing it by using the tools under the <functional> header. My code is as follows. class MainWindow { public: void Create() { WNDCLASSEXW WindowClass; WindowClass.cbSize = sizeof(WNDCLASSEX); WindowClass.style = m_ClassStyles; WindowClass.lpfnWndProc = std::function<LRESULT(HWND, UINT, WPARAM, LPARAM)> (