function-pointers

What kinds of C++ functions can be placed in a C function pointer?

∥☆過路亽.° 提交于 2020-06-11 22:33:01
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

不羁岁月 提交于 2020-06-11 22:32:34
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

南楼画角 提交于 2020-06-11 22:32:30
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

匆匆过客 提交于 2020-06-11 22:32:11
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

Is there a generic way to pass pointers of overloaded methods that requires less work (than my example)

二次信任 提交于 2020-03-23 08:05:37
问题 So I have a function where, using C++17, I'm able to apply any method from any object: #include <functional> template <typename Object, typename Method, typename ... Args> void ApplyMethod (Object && object, Method && method, Args && ... args) { std::invoke(method, object, args...); } What I ask: Is there a way to improve this to require less work for the caller of the function when the method is overloaded. Example use with overloaded methods: #include <iostream> class Foo { int bottles;

Why isn't std::function a valid template parameter while a function pointer is?

冷暖自知 提交于 2020-03-17 08:02:38
问题 I have defined class template named CallBackAtInit which only purpose is to call a function at its initialization (constructor). The function is specified in template parameters. The problem is that templates does not accept std::function as parameters; but they accept function pointers. Why? Here is my code: #include <iostream> #include <functional> /* Does not work:*/ template <typename return_type, typename arg_type, std::function<return_type(arg_type)> call_back> /* Work fine: *///

Function taking both pointer to member-function and pointer to const member-function

陌路散爱 提交于 2020-02-27 23:35:25
问题 I have the following code base: template <typename Type> class SomeClass { public: template <typename ReturnType, typename... Params> void register_function(const std::pair<std::string, ReturnType (Type::*)(Params...)> fct) { auto f = [fct](Params... params) -> ReturnType { return (Type().*fct.second)(std::ref(params)...); } // ... } }; This works when I pass a pointer to a member-function (non-const). However, if I want to pass a pointer to a const member-function, it results in a compile

c++ virtual function vs member function pointer (performance comparison)

╄→尐↘猪︶ㄣ 提交于 2020-02-02 14:30:51
问题 Virtual function calls can be slow due to virtual calls requiring an extra indexed deference to the v-table, which can result in a data cache miss as well as an instruction cache miss... Not good for performance critical applications. So I have been thinking of a way to overcome this performance issue of virtual functions yet still having some of the same functionality that virtual functions provide. I am confident that this has been done before, but I devised a simple test that allows the

c++ virtual function vs member function pointer (performance comparison)

僤鯓⒐⒋嵵緔 提交于 2020-02-02 14:30:07
问题 Virtual function calls can be slow due to virtual calls requiring an extra indexed deference to the v-table, which can result in a data cache miss as well as an instruction cache miss... Not good for performance critical applications. So I have been thinking of a way to overcome this performance issue of virtual functions yet still having some of the same functionality that virtual functions provide. I am confident that this has been done before, but I devised a simple test that allows the

Function Pointers with Different Return Types C

懵懂的女人 提交于 2020-01-29 17:47:22
问题 I understand what function pointers are in C as well as how to use them. However, I do not know how to have one function pointer that can point to functions with different return types. Is this possible? I know how to use an array of function pointers, but I have only found examples of different parameters, not return types. (C: How can I use a single function pointer array for functions with variable parameter counts?) If an array would make more sense, can you provide an example of how to