can function pointer point to member function of STL container - function

For example,
if I have
deque<int> a;
and I want to use a function pointer to a.front() , a.back() and a.push_front(), a.push_back(), can I accomplish that? If yes, how?

Pointers to member functions are a different type of function pointer than regular pointers, due to needing to pass this. The best solution is to implement a wrapper function as such:
template<typename T>
T deque_front(std::deque<T> *q) {
return q->front();
}

To call a (non-static) member function, you need an object to call it on (as well as the parameters of the function). This is implemented by having the member function take a (hidden)
extra parameter, this (effectively).
If you're using c++11 you can write a lambda to do this, or use std::bind to bind an instance of a deque and a member function together.

Related

In Rust, what is `fn() -> ()`?

I have a grasp of the Fn (capital-F) traits: Fn, FnMut, FnOnce. I understand that they are traits and work like traits.
But what about fn (lowercase-f)? It gets a different coloring in editors, which tells me it's not a trait. It can also be used in some places where the others can't (and vice-versa), though it seems to behave similarly in other cases. I couldn't find anything explaining it directly in the docs.
Rust has three kinds of function-like types:
Function items are what you get when you create a function by using fn foo() {...}. It's also the type of the constructor of a tuple-like struct or enum variant. Function items are zero-sized (they contain no data), and every non-generic function has a unique, unnameable function item type. In error messages, the compiler displays these "Voldemort types" as something like fn() -> () {foo} (with the name of the function in {}).
Closures are values similar to function items, but closures may contain data: copies of or references to whatever variables they capture from their environment. As you already know, you create a closure by using closure syntax (|args| expression). Like function items, closures have unique, unnameable types (rendered by the compiler something like [closure#src/main.rs:4:11: 4:23]).
Function pointers are what you're asking about: the types that look like fn() -> (). Function pointers cannot contain data, but they are not zero-sized; as their name suggests, they are pointers. A function pointer may point either to a function item, or to a closure that captures nothing, but it cannot be null.
Function items and closures are automatically coerced to the relevant function pointer type when possible, so that's why let f: fn(i32) = |_| (); works: because the closure captures nothing, it can be coerced to a function pointer.
All three function-like types implement the relevant Fn, FnMut and FnOnce traits (except that closures might not implement Fn or FnMut depending on what they capture). Function items and function pointers also implement Copy, Clone, Send and Sync (closures only implement these traits when all their contents do).
Performance-wise, function pointers are something of a compromise between generics and trait objects. They have to be dereferenced to be called, so calling a function pointer may be slower than calling a function item or closure directly, but still faster than calling a dyn Fn trait object, which involves a vtable lookup in addition to the indirect call. However, in real code there are many variables that confound naive analysis; if the difference in performance is important to you, you should measure it instead of guessing which is faster.
References
What's the practical difference between fn item and fn pointer?
Why design a language with unique anonymous types?
How do I make a struct for FFI that contains a nullable function pointer?
Why does passing a closure to function which accepts a function pointer not work?
It is a function pointer type.
It refers only to a function, not a closure, since it contains just the address of the function not the captured environment a closure needs.
A Fn trait (capital F) can refer either to a closure or a function.
fn is the type for a function pointer. See also here in the documentation:
https://doc.rust-lang.org/std/primitive.fn.html

How to pass a function AND its parameters into another function

My use case is this: a function called 'time' that will return how long it takes to run any function you give it.
So the time function needs to know all the parameters to pass into the function when it calls it.
I know how to pass a function into another function, but how can I pass all its parameters, without knowing in advance how many and what type they are, so they can be used when calling the function?
For example, if I pass in an array of all the parameters I need to send, is there some Dart way to call a function by expanding an array into a list of parameters? Or perhaps there's another way to capture and pass a function call, including all parameters, as one executable object?
I'm also interested in knowing if there's a more Dartful way to accomplish what I'm trying to do re: timing function calls.
I believe using a List of parameters with the apply method is the most common way and practical of doing this and I have seen something similar used to pass parameters for JS interop. As far as I know, there isn't a way to expand an array into a list of parameters like you can for javascript. You could of course create your own object to pass arguments, but I think it would add unnecessary complexity and end up being more difficult.
Example of passing parameters to function in dart:js here.

Pointer / Functions combination difficult to understand

What is the meaning of following statement.
((void*(*)(void*))keepfunc)(val)
Note:- In general I have problem understanding such expressions. Could, someone
Please suggest me some good material(web or book) for this?
You should try "parsing" the expression from inside out:
void*(*)(void*)
function pointer to a function expecting a pointer of void and returnning a pointer of void
(void*(*)(void*))keepfunc
cast to a function pointer expecting a pointer of void and returnning a pointer of void
((void*(*)(void*))keepfunc)(val)
This should be wrong, since a function pointer must be dereferenced before it can be called. It should look something like this:
(*(void*(*)(void*))keepfunc)(val)
Was this a working example from a textbook on C programming?
This is a cast that casts keepfunc to a function pointer that accepts a void* parameter and returns a void* result value, and then evaluates the function by passing val as the parameter (although, i think there should be an asterisk before keepfunc, because you need to deference the function pointer before calling it). I think you should just look up function pointers in google, that should clear things up.

Fortran: how to pass function name in a common block

In Fortran, is it possible to put a function in a common block as in:
COMMON /myblock/ func
(where x is some variable and func is a function).
My problem is that I would like to create a function s(x) that calls an external function func(x) but without passing func in s(x). For my project, s(x) has to be a function of only one variable, i.e., I do not want to do:
function s(x,func)
s=func(x)
Instead, I am hoping I could do:
function s(x)
common /myblock/ func
s=func(x)
Or, if someone has some other suggestion using modules or something, this will be great.
Thanks in advance for any help.
o.
and then have the same common (myblock) in the subroutine that calls s(x).
I don't believe that this is possible in any portable way. Some implementations may allow you to use some tricks to do it.
The modern way to do this is with a pointer to a function. The pointer could be passed as an argument or, for the design of this question, placed into a module. See, for example, Function pointer arrays in Fortran
I think you are not supposed to use common blocks for this, but modules. Put your function func in a module called myfunctions and then when needed insert at use myfunctions statement and thats it.
Modern fortran standards prohibit this. From 5.5.2 of Fortran 2003:
A common-block-object shall not be ... a function name, an entry name...
And at any rate, using global variables to pass around non-constant data is just a terrible, terrible idea. As ja72 points out, you could do this with modules, but I refuse to demonstrate it with sample code.

Checking anonymous function signatures at runtime (reflection) in AS3

Is there any way to have a look at signatures of anonymous functions in ActionScript 3 during runtime?
I would like to validate Function objects passed in as arguments to other functions and make sure that they accept the correct number of arguments (with the correct types) and return a value of the correct type.
flash.utils.describeType() doesn't seem to return this info for anonymous functions.
It doesn't look like the runtime allows you to reflect on anonymous functions, which is a shame.
Anonymous functions are (perhaps by definition) marked as dynamic. If you pass an incompatible type into an anonymous function, no error is thrown. Any type mismatches will be silently cast as best they can. Passing something like "minotaur" as a uint parameter will yield 0 (zero), for example.
If you REALLY want to over-engineer it, you could get all OO on it. Instead of accepting anonymous functions, you could declare an interface which contains the appropriate function signature, and accept objects that implement that interface.
public interface IFancyCallback {
public function fancyFunction(frog:Frog, princess:Girl):UsefulReturnType;
}
And your function would just have to be packaged up in an object:
public class ConcreteCallback implements IFancyCallback {
public function fancyFunction(frog:Frog, princess:Girl):UsefulReturnType {
princess.kiss(frog);
return new UsefulReturnType("jabberwocky");
}
}
Could have the potential to create a lot of code overhead. Depends how many callbacks you intend to have, who's going to be making them, and what how serious it would be if the anon function's signature was incorrect.
Can you share any more about the problem you're trying to solve?
As a rough approximation you can check the number of parameters using length property, e.g.:
function doStuff(callback:Function) {
trace(callback.length);
}
I'm not aware of any way to enumerate the arguments of an anonymous functions; you can of course validate ex-post catching ArgumentError.