FAQ Database Discussion Community


Call wrapper inside shared_ptr<> instance on its member function

c++,shared-ptr,std-function,stdbind
I'm trying to make a forwarding call wrapper with std::bind() of an internal member function inside instance which has been created as a shared_ptr<>. Look like there's no chance. In a nutshell: std::shared_ptr<Parent> _instance(std::make_shared<Child>()); _instance->init(); _instance->proc(); // class Parent : std::enable_shared_from_this<Parent> { protected: std::vector<std::function<void()>> _vector; public: virtual void init() =...

How to implement strategy pattern in C++ with std::function

c++,pointers,c++11,strategy-pattern,std-function
I'm reasing about the best way to implement the strategy pattern in C++. Up to now, I've always used the standard way, where the context has a pointer to the base strategy class as follows: class AbstractStrategy{ public: virtual void exec() = 0; } class ConcreteStrategyA{ public: void exec(); }...

Is there a better way to do this than writing a wrapper allocator that stores a reference to a stateful allocator object?

c++,memory-management,lambda,allocator,std-function
For example: struct Foo { MyPoolAlloc<char> pool; std::vector<int , MyPoolAlloc<char>> vec_int; // The wrapper allocator would replace MyPoolAlloc<> here. std::vector<std::function<void()> , MyPoolAlloc<char>> vec_funcs; // The wrapper allocator would replace MyPoolAlloc<> here. Foo() : vec_int(pool) , vec_funcs(pool) {} // I want to store lambdas with captured variables using the custom allocator...

In C++11, is it possible to wrap a template function in a std::function?

c++,templates,c++11,std-function,stdbind
If i have a template function in C++11, is it then possible to wrap it in a std::function? My problem is like this: I have a generic function (say a sum function) where the return type depends on the types of the arguments: template <typename T> auto sum(T const& a,...

Accept std::function with arbitrary inputs as input w/o Templates

c++11,math,input,lambda,std-function
For Learning Purposes: I am creating a small numerical methods library and I am trying to implement the gradient currently I have done 2D gradient and 3D gradient . But I want to generalize this to higher dimensions. Currently I have : matrix<double> analysis::gradient_2D(std::function<double(double, double)> fn, double x, double y)...

Using std::function in member initialization list

c++,c++11,std-function,stdbind,member-initialization
I have a typedef: typedef S32(iMyDataClass1::*getDataFunction_t)(void); and a type: struct functionMap_t { std::vector<getDataFunction_t> pDataFunctionTable; struct dataRequestor_t dataRequestor; }; I then have a member variable: functionMap_t FnMap1; Which I then set up in an member initializer list: myClass::myClass() : FnMap1({ { &iMyDataClass1::getData1, &iMyDataClass1::getData2, &iMyDataClass1::getData3 }, dataRequestor1 }) Which I then use...

Initializing class with a lambda passed into constructor, C++11

c++11,constructor,std-function
Lets consider following example: #include <functional> #include <iostream> using namespace std; class Caller { public: Caller(function<void()> callback) { callback(); } }; main() { #if defined(ONELINER) Caller caller = [] { cout << "test"; }; #else function<void()> fun = [] { cout << "test"; }; Caller caller(fun); #endif // defined(ONELINER) }...

std::function fails to distinguish overloaded functions

c++,c++11,overload-resolution,std-function
I am trying to understand why std::function is not able to distinguish between overloaded functions. #include <functional> void add(int,int){} class A {}; void add (A, A){} int main(){ std::function <void(int, int)> func = add; } In the code shown above, function<void(int, int)> can match only one of these functions and...

std::vector that holds class methods

c++,vector,enums,std,std-function
I have done this to enumerate my class members enum MemberType {A, B, C, D}; class Hello { public: std::vector<std::function<void(Hello*, void)>> m_members; void func() { }; Hello() { m_members[A] = func(); } }; What is wrong here?...

std::function with C variadic arguments, not templated variable arguments

c++,c++11,varargs,variadic-functions,std-function
I basically want to do the following: typedef std::function<int(const char* format, ...)> PrintfLikeFunction; However, that does not seem to work. Is that possible? I really need to match functions with variadic arguments in my case so that I could pass in printf. The other reason is, I want to have...

VS2012 std::function operator bool returns true unexpectedly

c++,visual-studio-2012,signature,callable,std-function
I have come across a strange behavior in VS2012 with std::function that I couldn't find anything about online. Given the following sample program: #include <functional> #include <iostream> typedef std::function< int()> fntype; void foo(const fntype& fn) { try { if(fn) fn(); else std::cout << "fn is empty" << std::endl; } catch(const...

In whats case should use auto type?

c++,lambda,auto,c++14,std-function
I have some questions regarding when use auto Explicit, it is not more clear despite the context? Cases that are better be explicit instead of auto? And with lambda functions use auto how these auto f = [](auto v, auto x) {}; //or std::function<void(auto v, auto x)> f; Thanks......