Isn’t the template argument (the signature) of std::function part of its type?

The problem is that both function<int()> and function<int(int)> are constructible from the same function. This is what the constructor declaration of std::function looks like in VS2010: template<class _Fx> function(_Fx _Func, typename _Not_integral<!_Is_integral<_Fx>::value, int>::_Type = 0); Ignoring the SFINAE part, it is constructible from pretty much anything. std::/boost::function employ a technique called type erasure, to allow … Read more

C++11 lambda as data member?

A lambda just makes a function object, so, yes, you can initialize a function member with a lambda. Here is an example: #include <functional> #include <cmath> struct Example { Example() { lambda = [](double x) { return int(std::round(x)); }; }; std::function<int(double)> lambda; };

Should I copy an std::function or can I always take a reference to it?

Can I store the function as a reference since std::function is just a function-pointer and the ‘executable code’ of the function is guaranteed to stay in memory? std::function is very much not just a function pointer. It’s a wrapper around an arbitrary callable object, and manages the memory used to store that object. As with … Read more

C++11 std::set lambda comparison function

It’s unlikely that the compiler will be able to inline a std::function call, whereas any compiler that supports lambdas would almost certainly inline the functor version, including if that functor is a lambda not hidden by a std::function. You could use decltype to get the lambda’s comparator type: #include <set> #include <iostream> #include <iterator> #include … Read more

What is the purpose of std::function and how to 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 properly check if std::function is empty in C++11?

You’re not checking for an empty lambda, but whether the std::function has a callable target stored in it. The check is well-defined and works because of std::function::operator bool which allows for implicit conversion to bool in contexts where boolean values are required (such as the conditional expression in an if statement). Besides, the notion of … Read more

std::function and std::bind: what are they, and when should they be used?

std::bind is for partial function application. That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b); g is a “partial application” of the function f: the middle argument has already been specified, and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)