C++ function types

Here’s the relevant paragraph from the Standard. It pretty much speaks for itself. 8.3.5/10 A typedef of function type may be used to declare a function but shall not be used to define a function (8.4). Example: typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void … Read more

template argument deduction/substitution failed, when using std::function and std::bind

To figure out the problem let separate statements: auto f = bind(&TestA::testa, &testA, _1, _2); // OK test.setCallback(f); // <<— Error is here setCallback needs to know type of T and it can’t deduce it from f, so give it a type test.setCallback<TYPE>(f); // TYPE: int, float, a class, …

The reasoning behind Clang’s implementation of std::function’s move semantics

It is a bug in libc++ that cannot be immediately fixed because it would break ABI. Apparently, it is a conforming implementation, although obviously it is often suboptimal. It’s not clear exactly why the Clang devs made such an implementation choice in the first place (although maybe if you’re really lucky, someone from Clang will … Read more

How are C++11 lambdas represented and passed?

Disclaimer: my answer is somewhat simplified compared to the reality (I put some details aside) but the big picture is here. Also, the Standard does not fully specify how lambdas or std::function must be implemented internally (the implementation has some freedom) so, like any discussion on implementation details, your compiler may or may not do … Read more

What is the purpose of std::function and how do I use it?

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function, the primary1 operations are copy/move, destruction, and ‘invocation’ with operator() — the ‘function like call operator’. In less abstruse English, it means that std::function can contain almost … Read more

How to directly bind a member function to an std::function in Visual Studio 11?

I think according to the C++11 standard, this should be supported Not really, because a non-static member function has an implicit first parameter of type (cv-qualified) YourType*, so in this case it does not match void(int). Hence the need for std::bind: Register(std::bind(&Class::Function, PointerToSomeInstanceOfClass, _1)); For example Class c; using namespace std::placeholders; // for _1, _2 … Read more

Can a std::function store pointers to data members?

The effect of a call to the function call operator of std::function<R(ArgTypes…)>: R operator()(ArgTypes… args) const is equivalent to (§ 20.9.11.2.4 [func.wrap.func.inv]/p1): INVOKE<R>(f, std::forward<ArgTypes>(args)…) whose definition includes the following bullet (§ 20.9.2 [func.require]/p1): Define INVOKE(f, t1, t2, …, tN) as follows: […] 1.3 — t1.*f when N == 1 and f is a pointer to … Read more