std-function

What is the return type of a lambda expression if an item of a vector is returned?

吃可爱长大的小学妹 提交于 2019-11-28 10:42:31
Consider the following snippet: #include <iostream> #include <vector> #include <functional> int main() { std::vector<int>v = {0,1,2,3,4,5,6}; std::function<const int&(int)> f = [&v](int i) { return v[i];}; std::function<const int&(int)> g = [&v](int i) -> const int& { return v[i];}; std::cout << f(3) << ' ' << g(3) << std::endl; return 0; } I was expecting the same result: in f , v is passed by const reference, so v[i] should have const int& type. However, I get the result 0 3 If I do not use std::function, everything is fine: #include <iostream> #include <vector> #include <functional> int

C++ function types

南楼画角 提交于 2019-11-28 09:46:59
I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function ): typedef int Signature(int); // the signature in question typedef std::function<int(int)> std_fun_1; typedef std::function<Signature> std_fun_2; static_assert(std::is_same<std_fun_1, std_fun_2>::value, "They are the same, cool."); int square(int x) { return x*x; } Signature* pf = square; // pf is a function pointer, easy Signature f; // but what the hell is this? f(42); // this compiles but doesn't link The variable f can not be assigned, but can be called. Weird. What is it

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar? [duplicate]

自作多情 提交于 2019-11-28 09:13:57
This question already has an answer here: Why do we use std::function in C++ rather than the original C function pointer? [duplicate] 3 answers The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers? std::function<> gives you the possibility of encapsulating any type of callable object , which is something function pointers cannot do (although it is true that non-capturing lambdas can be converted to function pointers). To

Using 'void' template arguments in C++

折月煮酒 提交于 2019-11-28 08:17:40
Take the following minimal example: using Type1 = std::function<void(void)>; template <typename T> using Type2 = std::function<void(T)>; Type1 whyDoesThisWork; Type2<void> andYetThisDoesNot; If the second type alias, I get the error "Argument may not have 'void' type". (I tested with Xcode 4.5, Clang/c++11/libc++, OS X 10.7.) I find this curious: I would have expected Type1 and Type2<void> to behave identically. What's going on here? And is there a way to rewrite the second type alias so I can write Type2<void> and get std::function<void(void)> instead of an error? Edit I should probably add

Should std::function assignment ignore return type? [duplicate]

余生颓废 提交于 2019-11-28 08:13:48
问题 This question already has answers here : Using `std::function<void(…)>` to call non-void function (3 answers) Closed 5 years ago . Is the code below valid C++ according to the C++11 or C++14 standard? #include <functional> int ReturnInt() { return 5; } int main( int argc, char **argv ) { std::function< void () > BoundType = ReturnInt; return 0; } The code compiles fine with the latest cygwin versions of gcc (4.8.3) and clang (4.3.2) but not with Visual Studio 2013, Visual Studio November 2013

Comparing std::functions for equality?

梦想与她 提交于 2019-11-28 08:03:53
How can I compare two C++11 std::function s with operator== , and return true if both of said function s refer to the same function pointer? operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why. Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well. Quoting the FAQ: Comparison between boost::function objects cannot be implemented "well", and therefore will

c++11: How to write a wrapper function to make `std::function` objects

北城余情 提交于 2019-11-28 07:45:51
I am trying to write a wrapper make_function , which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair , for a function pointer foo , auto f0 = make_function(foo); creates a std::function function object f0 of the right type signature. Just to clarify, I don't mind occasionally giving type parameters to make_function in case it is difficult (or impossible) to deduce the type entirely from the parameters. What I came up with so far (code below) works fine for lambdas, some function pointers, and functors (I didn't consider volatiles).

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

旧时模样 提交于 2019-11-27 22:53:05
I can easily bind member functions to a std::function by wrapping them with a lambda expression with capture clause. class Class { Class() { Register([=](int n){ Function(n); }); } void Register(std::function<void(int)> Callback) { } void Function(int Number) { } }; But I want to bind them directly, something like the following. // ... Register(&Class::Function); // ... I think according to the C++11 standard, this should be supported. However, in Visual Studio 11 I get these compiler errors. error C2440: 'newline' : cannot convert from 'int' to 'Class *' error C2647: '.*' : cannot dereference

Vector of std::function with different signatures

别等时光非礼了梦想. 提交于 2019-11-27 20:41:25
I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions. e.g. void func1(const std::string& value); void func2(const std::string& value, int min, int max); const std::vector<std::function<void(std::string)>> functions { func1, func2, }; I realise the above isn't possible, but I wonder if there are any alternatives I should consider. I haven't been able to find any yet, and I've experimented with std::bind but not managed to achieve what I want. Is such a thing possible? You

std::function template argument resolution

喜欢而已 提交于 2019-11-27 18:16:47
问题 I am currently working on a library where I am chaining function objects. I am creating a function template that takes a callable object (std::function at the moment) and is parametrized on the output and input type of the function. Here is a simplified version of what I am defining: template <typename In, typename Out> std::vector<Out> process(std::vector<In> vals, std::function< Out(In) > func) { // apply func for each value in vals return result; } The problem I am having is on usage. It