std-function

Why can std::function be constructed with a lambda with a different return type?

不羁的心 提交于 2019-12-08 17:05:27
问题 The following compiles fine: #include <functional> int main() { std::function<const int&()> f = []() -> int {return 1;}; const int& r = f(); // r is a dangling reference return 0; } How come it's possible to set an std::function with a const int& return type to a lambda with an int return type? Allowing this sort of cast to happen implicitly and with no warning is a gotcha IMHO. 回答1: You can construct a std::function with any object which is callable with the relevant arguments and whose

Converting std::function<void(Derived*)> to std::function<void(Base*)>

余生长醉 提交于 2019-12-08 14:53:07
问题 First, I define two classes, which inherits from one another. class A { }; class B : public A { }; Then, I declare a function that uses an std::function<void(A*)> : void useCallback(std::function<void(A*)> myCallback); Finally, I receive a std::function of a different (but theoretically compatible) type from somewhere else that I would like to use in my callback function: std::function<void(B*)> thisIsAGivenFunction; useCallback(thisIsAGivenFunction); My compiler (clang++) refuses this

Storing generic std::functions in a STL map?

ε祈祈猫儿з 提交于 2019-12-08 09:48:44
问题 I've a bunch of delegate factories, defined as Lambdas using different arguments, i.e.: std::function<Mesh*()> f1 = [&]() -> Mesh * {return new Mesh();}; std::function<Image*(const std::string&)> f2 = [&](const std::string& path) -> Image * {return new Image(path);}; std::function<VertexBuffer*(VertexBuffer::eType, int, int)> f3 = [&](VertexBuffer::eType type, int size, int count) -> VertexBuffer * {return new VertexBuffer(type, size, count);}; With those delegates I could create different

How to avoid explicit cast with std::bind() temporary objects?

有些话、适合烂在心里 提交于 2019-12-08 04:25:58
问题 The return type of std::bind is (intentionally) unspecified. It is storable in a std::function. The example program below shows how I have to explicitly cast the temporary object returned by std::bind() to a std::function in order to call fn1(). If the return type of std::bind was knowable, I could overload the Callback constructor & would no longer need to explicitly cast std::bind temporary objects. Is there any way to avoid the explicit cast? // g++ -std=c++11 test.cxx #include <functional

How to avoid explicit cast with std::bind() temporary objects?

孤街醉人 提交于 2019-12-08 03:50:28
The return type of std::bind is (intentionally) unspecified. It is storable in a std::function. The example program below shows how I have to explicitly cast the temporary object returned by std::bind() to a std::function in order to call fn1(). If the return type of std::bind was knowable, I could overload the Callback constructor & would no longer need to explicitly cast std::bind temporary objects. Is there any way to avoid the explicit cast? // g++ -std=c++11 test.cxx #include <functional> using std::placeholders::_1; class A { public: void funcA (int x) { } }; class Callback { public:

assigning std::function to a member function

被刻印的时光 ゝ 提交于 2019-12-07 13:38:19
问题 class A { public: std::function<void(int)> f_; void print_num(int i) { cout << i; } void setFuntion(std::function<void(int)> f) { f_=f; } void run() { setFunction(print_num); } }; this doesn't work. i get note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::function<void(int)>’ and other errors. If I put the definition of print_num outside of the class. everything works. i tried adding &A:: , A:: and this. nothing helped. 回答1: print_num is a non

How to make these std::function parameters unambiguous?

谁都会走 提交于 2019-12-07 11:47:55
问题 The following function overloads are ambiguous when passing a lambda. I found out that std::function can be constructed from most callable types, even if their signature does not match. So the compiler can't tell which function to use. template <typename T> void each(std::function<void(T)> iterator); template <typename T> void each(std::function<void(T, id)> iterator); template <typename T> void each(std::function<void(T&)> iterator); template <typename T> void each(std::function<void(T&, id)

How can I use polymorphism with std::function?

為{幸葍}努か 提交于 2019-12-07 11:38:05
问题 Let's say I have 2 classes: class A {} class B : public A {} And i want to use an std::function the receives anything of type A , but with assign to it methods that receive classes that inherit from A (like B ). void myFun(B bbbb) {} std::function<void(A)> blah = std::bind(myFun, _1); This obviously doesn't work, because the compiler won't just downcast implicitly. But how can I do something like this ? Basically I want to hold a map of some basic std::function type, and in each mapped value

Initialize class containing a std::function with a lambda

柔情痞子 提交于 2019-12-07 08:12:20
问题 I created a template class containing a std::function as a member the following way: template<typename Ret, typename... Args> class Foo { private: std::function<Ret(Args...)> _func; public: Foo(const std::function<Ret(Args...)>& func): _func(func) {} }; In order not to have to specify the arguments and return type of the passed function, I created some make_foo overloads: template<typename Ret, typename... Args> auto make_foo(Ret (&func)(Args...)) -> Foo<Ret, Args...> { return { std::function

What's the point of std::function constructor with custom allocator but no other args?

好久不见. 提交于 2019-12-06 21:07:39
问题 I'm playing around with std::function and custom allocators but its not behaving as I expected when I don't provide the function with an initial functor. When I provide a custom allocator to the constructor but no initial functor, the allocator is never used or so it seems. This is my code. //Simple functor class that is big to force allocations struct Functor128 { Functor128() {} char someBytes[128]; void operator()(int something) { cout << "Functor128 Called with value " << something <<