std-function

Have the ideas behind the Fast Delegate (et al) been used to optimize std::function?

梦想的初衷 提交于 2019-12-18 11:48:44
问题 There have been proposals for C++ "delegates" which have lower overhead than boost::function : Member Function Pointers and the Fastest Possible C++ Delegates Fast C++ Delegate The Impossibly Fast C++ Delegates Have any of those ideas been used to implement std::function , resulting in better performance than boost::function ? Has anyone compared the performance of std::function vs boost::function ? I want to know this specifically for the GCC compiler and libstdc++ on Intel 64-bit

Construct std::function with a constructor based on template

泄露秘密 提交于 2019-12-18 08:25:54
问题 Is it possible to construct a std::function with the constructor of a type defined by a template argument? For example: template <typename T> bool registerType() { const std::function<T()> func = &T::T; //I know this doesn't work //... } 回答1: I don't think so, because constructors don't have names, you can't take a pointer/reference to them, and in general they don't behave quite like functions. You could use a lambda to initialize a std::function with the same signature: const std::function

std::function instead of templates for predicates

孤者浪人 提交于 2019-12-18 05:49:28
问题 Many standard library algorithms take predicate functions. However, the type of these predicates is an arbitrary, user-provided template parameter. Why doesn't C++11 specify that these take a specific type, like std::function instead? For example: template< class InputIt > InputIt find_if( InputIt first, InputIt last, std::function<bool()> p ); Isn't using this instead of a template as argument type not much more clean? 回答1: std::function is for runtime polymorphism. Any particular std:

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

我的梦境 提交于 2019-12-17 18:53:23
问题 This question already has answers here : Why do we use std::function in C++ rather than the original C function pointer? [duplicate] (3 answers) Closed 6 years ago . 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? 回答1: std::function<> gives you the possibility of encapsulating any type of callable object , which is

Using 'void' template arguments in C++

烂漫一生 提交于 2019-12-17 18:34:58
问题 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

Deduce template argument from std::function call signature

北慕城南 提交于 2019-12-17 05:11:21
问题 Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */ } foo<bool>(bar); // works foo(bar); // error: no matching function call 回答1: std::function<bool()> bar; foo(bar); // works just fine C++ can't deduce the return type from your function bar because it would have to know the type before it could find all the

Storing arbitrary function objects into a class member container without knowing their declaration signature

房东的猫 提交于 2019-12-13 17:22:50
问题 I have a class template, and I do not know if I need to have as one of its arguments class Func or not. I also do not know if I can arbitrarily store a std::function directly into a container without its template argument parameter list. My class looks something like: template<class Data, class Func> class ThreadManager final { private: std::vector<std::shared_ptr<Data>> storedDataPtrs; std::vector<std::shared_ptr<std::thread>> storedThreadPtrs; // Is this valid? I know that `std::function is

Executing bound std::function throws std::bad_function_call

牧云@^-^@ 提交于 2019-12-13 09:01:20
问题 I want to bind a member function to a std::function<void(void)> . I heard that member functions take one extra parameter which is the instance pointer. Therefore I call std::bind(&Class::Function, this, parameter) but when I execute the function object, it throws a runtime error. Unhandled exception at at 0x748D4B32 in Application.exe: Microsoft C++ exception: std::bad_function_call at memory location 0x0114F4E8. The parameter is a pointer to one of my own struct s. How am I doing wrong? What

Can std::function be serialized?

北战南征 提交于 2019-12-12 10:36:07
问题 This is a theoretical question. Say there are some objects which among others contain lists of callback functions subscribed to events of those objects. Now we want to store those objects on disk. Is a std::function serializable? 回答1: No. Whenever using type erasure (ie, hiding implementation details behind an interface), the only operations available without knowing the dynamic type of the object are those provided by the interface. There is no serialization in the C++ Standard, and there is

Passing a member function via std::function

笑着哭i 提交于 2019-12-12 02:27:23
问题 I want to check if a data field is valid (valid means being not null and not filled with the default value) Basically return (!connector->IsNull(field_id) and connector->Get<type>Default(field_id, default_value)) But "type" can be one of many types (string, int64, etc...) so there are 5-6 different functions. I made a helper function for it and I'm trying to pass in the relevant GetDefault... template<typename T> bool IsValidField(std::unique_ptr<Connector>& connector, const std::function<T