Function pointer vs Function reference
Functions and function references (i.e. id-expressions of those types) decay into function pointers almost immediately, so the expressions func
and f_ref
actually become function pointers in your case. You can also call (***func)(5)
and (******f_ref)(6)
if you like.
It may be preferable to use function references in cases where you want the &
-operator to work as though it had been applied to the function itself, e.g. &func
is the same as &f_ref
, but &f_ptr
is something else.
What is the difference between a function call and function reference?
Well, the onclick
property expects a reference to a function, for it to execute when the element is clicked. Normally it's either:
element.onclick = funcRef;
or
element.onclick = function () {
funcRef();
};
(but of course, it's best to use addEventListener
and attachEvent
)
Notice how both of them are references to functions, not calling.
When something expects a reference, you don't call it...you assign a reference to it (first example).
When you want to specifically call a function, you call it with ()
(second example). But notice how in the second example, there's still a reference to a function that's assigned to onclick
- it's just an anonymous function.
Probably the more important part:
Some people think you want to do this:
element.onclick = funcRef();
But that immediately executes the function (because of the ()
), and assigns its return value to onclick
. Unless the return value is a function, this isn't what you want.
I think the moral of the story is that when you want/need something to execute right now, you call the function. If the function is wanted for later use or needs stored, you don't call it.
Is it possible to reserve function reference into unordered_map or vector?
Regardless of wether this is something you should be doing or not (the comments under your question are covering this already), it's still worth answering your question as is.
The []
operator of map types is a veritable swiss army knife. You can do a lot of things with it.
- You can assign a value.
- You can lookup the value.
- You can lookup a value even if it doesn't exist yet.
Because of this, using that operator imposes some requirements for whatever type is stored in the map. In this specific case, you are running into the requirement that it has to be value-initializable, which references cannot be.
This applies to regular references as well by the way, not just function references.
So the answer is: You can store references in a map as much as you like as long as you don't use any function of the map that requires the reference to do something it's not allowed to.
In this case, you can use umap.at(0)(10.1);
. One of the big differences between []
and at()
is that if the key is not set yet, at()
will throw an exception instead of creating a value-initialized value.
Comparison operator and function references
I believe the comments have been explanatory, the grouping operator (..)
is necessary not only for precedence but also, as demonstrated in the example from the Doc, it allows you to let output from a command participate in an expression.
Without it, PowerShell interprets -eq
as a parameter of the function mj
and 23
as an argument for said parameter.
You can test this by adding $args
or a parameter that takes ValueFromRemainingArguments
to your function:
function mj { 23; $args }
function mjWithRemainingArgs {
param(
[parameter(ValueFromRemainingArguments)]
$remargs
)
23; $remargs
}
PS /> mj -eq 23
23
-eq
23
PS /> mjWithRemainingArgs -eq 45
23
-eq
45
Why does passing functions by value work but not by reference
You are trying to bind a non-constant reference with a temporary object.
You could use a constant reference.
Here is a demonstration program.
#include <iostream>
#include <functional>
void foo( const std::function<int(int)> &stuff )
{
int x = 10;
std::cout << stuff( x ) << '\n';
}
int main()
{
auto fct = [](int x){return x * 10;};
foo(fct);
}
The program output is
100
Without the qualifier const you could write for example
#include <iostream>
#include <functional>
void foo( std::function<int(int)> &stuff )
{
int x = 10;
std::cout << stuff( x ) << '\n';
}
int main()
{
auto fct = [](int x){return x * 10;};
std::function<int(int)> f( fct );
foo(f);
}
As for the lambda-expression then according to the C++ 17 Standard (8.1.5.1 Closure types)
1 The type of a lambda-expression (which is also the type of the
closure object) is a unique, unnamed non-union class type, called the
closure type, whose properties are described below.
Perl dynamic / symbolic function reference
All you need is this:
my $ref = \&{ "${package}::$name" };
or
my $ref = \&{ $package . "::" . $name };
Using an expression producing the name of a variable where a variable name is expected is called a symbolic reference. These are usually forbidden by use strict;
(specifically use strict qw( refs );
), but \&
is exempt.
Related Topics
Can MACros Be Overloaded by Number of Arguments
Converting Data from Glreadpixels() to Opencv::Mat
Compiling with -Static-Libgcc -Static-Libstdc++ Still Results in Dynamic Dependency on Libc.So
How Can Std::Make_Heap Be Implemented While Making at Most 3N Comparisons
How to Read Groups of Integers from a File, Line by Line in C++
Debugging Information Cannot Be Found or Does Not Match Visual Studio'S
Vector of Std::Function with Different Signatures
How to Store Arrays in an Stl List
Is It Unspecified Behavior to Compare Pointers to Different Arrays for Equality
Downcasting Shared_Ptr<Base> to Shared_Ptr<Derived>
How to Alpha Blend Rgba Unsigned Byte Color Fast
What Destructors Are Run When the Constructor Throws an Exception
Why Does This Program Crash: Passing of Std::String Between Dlls
Writing a Simple Equation Parser
On Local and Global Static Variables in C++
C++ How to Generate All the Permutations of Function Overloads