Questions tagged [function-object]
In object-oriented languages, function object (also known as functor) is a feature that allows objects to be used like if they were ordinary functions.
163
questions
1
vote
1
answer
79
views
Can a javascript Function object be called?
This is weird! I am struggling hard with the problem that javascript Proxy handler's apply trap does not get the Proxy itself passed as an argument, only the naked function. But I need to pass along ...
0
votes
1
answer
71
views
Execution speed of code with `function` object as compared to using template functions
I know that std::function is implemented with the type erasure idiom. Type erasure is a handy technique, but as a drawback it needs to store on the heap a register (some kind of array) of the ...
21
votes
2
answers
2k
views
What does static_cast mean when it's followed by two pairs of parentheses?
What does this say:
return static_cast<Hasher &>(*this)(key);
?
I can't tell whether *this or key is passed to static_cast. I looked around and found this answer, but unlike what I'm stuck ...
0
votes
0
answers
72
views
Attempting to use global variable in javascript Function object
Putting this at the top of my post for a little extra clarity:
The overall goal here is to build a feature for dynamic banners where you can input some custom javascript animation code through a ...
2
votes
1
answer
164
views
unary_function and binary_function are deprecated in C++ 11, and removed in C++ 17. What should we use instead? [duplicate]
I have been reading Effective STL by Meyers.
I came across some sections which mention function adapter objects, such as not1, bind1st, bind2nd. There are apparently a range of such function adapter ...
0
votes
1
answer
56
views
Does Boost (or another library) offer a way to lift the name of a "constructor-less" class into a function object that uses aggregate initialization?
This is kind of a follow up to this question, where I asked how I could tersely turn a template and/or overloaded function into a function object.
The accepted answer was you can't do without macro, ...
0
votes
1
answer
74
views
Add index signature to a function in .d.ts
I want to overwrite npm library typing to add index signature to a function. Let's say the function does nothing spectacular:
export function foo(input) {
return Number(input);
}
It has a typing in ...
1
vote
2
answers
78
views
How to run do_n?
I'm currently working on recursive function on Think Python, page 44.
It wrote:
"Write a function called do_n that takes a function object and a number, n as arguments,
and that calls the given ...
0
votes
0
answers
79
views
How do I use BOOST_HOF_LIFT and BOOST_HOF_LIFT_CLASS with MSVC?
(This is in part a follow up to this question of mine.)
As I've written in this self-answer, I've discovered that Boost offers a macro to wrap a template function in a function object so it can be ...
0
votes
1
answer
104
views
how to use a function object as a custom comparator for accessing a local variable instead of using a lambda function in C++?
I am trying to learn priority_queue concept in C++, and I came across this interview question. Although, I managed to solve this problem with a lambda function, I could not figure out how to do the ...
1
vote
3
answers
210
views
STL and std custom compare arguments working in c++
I asked this question on stackoverflow STL passing object
I got to know that we pass objects which in tern call the compare operator in them and compare our values and gives us a result. All good.
Now ...
0
votes
0
answers
33
views
If I define a move constructor and don't need a copy constructor, why would I need to define a copy constructor? [duplicate]
Using Apple clang version 12.0.0 (clang-1200.0.32.29) -std=c++17,
the following code does not compile
struct C {
C() {}
C(C&& other) {std::cout << "move\n";}
//C(const C&...
0
votes
1
answer
55
views
Why is count_if giving me the total of texts
I was testing the following code, and a bit perplexed as to why count_if is returning me the total of texts?
Match function that takes string of Text as argument and returns true is the Text size is 4
...
2
votes
2
answers
42
views
Functional Object in Polymorphism
I want to implement Functional Object in Polymorphism as follows:
#include <algorithm>
#include <iostream>
using namespace std;
struct Compare {
virtual bool operator() (int, int) ...
4
votes
2
answers
57
views
Is there a way to call std::equal_range with a std::function instead of a structure?
In the below example a struct Comp is created. Is there a way to make it a std::function or write the comparator itself while calling std::equal_range? Can I get rid of the struct?
struct Comp
{
...
0
votes
1
answer
52
views
store a function without directly creating new function objects in C++ way
I'm trying to create a generic menu, so I came with the idea of creating a menu with options, and each option in the menu will have a label, key to execute, and an Action.
The Action is template class ...
3
votes
3
answers
207
views
Most terse and reusable way of wrapping template or overloaded functions in function objects
Scenario 1: a template function pred
template<typename T>
bool pred(T t) { /* return a bool based on t */ }
Scenario 2: a set of functions overloaded on the same name pred
bool pred(A t) { /* ...
1
vote
1
answer
106
views
How to sort a vector with given column only on a triplet value? [duplicate]
I have a struct with three int in it.
struct x{
int a,
b,
c;
};
I am using the struct to store triplets in the vector as the triplets will represent source, destination and weight.
vector<...
2
votes
1
answer
104
views
Proper way of returning a functor in C++
Consider we have an add function declared like this:
int add(const int a, const int b);
If we were to return this add function from foo...
std::function<int(const int, const int)> foo()
{
...
2
votes
1
answer
64
views
Function object to pass std::get around as an argument to functions
My intention is to write a getter for the Nth element of whatever std::get can take as argument once it's instantiated with a specific N.
In other words, std::get<N> cannot be passed around ...
2
votes
1
answer
246
views
Function ignoring its argument(s)
This is mostly a curiosity question.
I saw code like this (I'm not interested in what it does)
constexpr auto xxx = boost::hana::overload(
[](SomeType& x){ /* x is used and something is ...
0
votes
1
answer
104
views
Cannot find symbol of written method java.util.function
I have code like
public class Functionz {
public static boolean test() {
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {test}; /...
1
vote
1
answer
238
views
Using smart pointers within multi-threaded application
I have some calculation I need to perform that is dependent on two or more steps as follows:
class A
{
public:
double step1() { return 2.5; }
};
class B
{
public:
double step2() { return 1.2; ...
1
vote
2
answers
51
views
Passing function objects and using them in other functions in Python 3.8.4
I was looking at this question:
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) ...
1
vote
0
answers
86
views
Why C++ standard library does not pass predicates as && [duplicate]
In C++ stl, there is e.g.:
template<class InputIt, class UnaryPredicate>
constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
My question is: why there is not:
... ...
2
votes
1
answer
126
views
How can I compare std::function objects?
I have a vector of std::function objects defined like this:
std::vector<std::function<void()>> funcs = { myFunc1, myFunc2, myFunc3 };
//The functions are defined like this:
//void myFunc1(...
0
votes
1
answer
61
views
I wonder that if the temporary object(name_compare()) is passed by value or by reference to std::sort
I wonder that if the temporary object(name_compare()) is passed by value or by reference to std::sort.
I would be thankful for any hint on this question.
struct Record {
string name;
...
0
votes
1
answer
35
views
Does `name_compare()` calls the default construction or the `operator()` of `class name_compare` in `sort(vs.begin(), vs.end(), name_compare());`?
Does name_compare() calls the default construction or the operator() of class name_compare in sort(vs.begin(), vs.end(), name_compare());?
I think it's the first one.Am i right?
I would be thankful ...
2
votes
0
answers
82
views
C++ primer 5. ed. A lambda closure in place of a function pointer not allowed?
In C++ primer chapter 14. Function objects:
// ordinary function
int add(int i, int j) { return i + j; }
// lambda, which generates an unnamed function-object class
auto mod = [](int i, int j) { ...
3
votes
3
answers
195
views
C++ primer 5 ed. : using sort on a vector of pointers is it undefined?
In C++ primer 5 ed. page 172:
"We cannot use the relational operators on pointers to two unrelated objects:
int i = 0, sz = 42;
int *p = &i, *e = &sz;// undefined: p and e are unrelated; ...
0
votes
0
answers
68
views
Call function object from doIt function
#include <iostream>
void hello1() {
std::cout << "Hello from normal\n";
}
auto hello2 = [] {
std::cout << "Hello from lamda\n";
};
class hello
{
public:
hello() {}
...
0
votes
2
answers
499
views
Is it possible to override Function.prototype.toJSON so that JSON.stringify could work with functions?
Or maybe even override some portion of JSON.parse to parse functions?
This isn't time sensitive, I built work arounds in my code, but with the eval function, you would think that turning functions ...
1
vote
0
answers
40
views
Retrieve parameter types of all overloads of callable operators of function object
#include <iostream>
#include <string>
struct foo
{
void operator()( const int& ) { }
void operator()( const float& ) { }
void operator()( const std::string& ) { }
...
19
votes
4
answers
4k
views
Overload a lambda function
How to overload a simple local lambda function?
SSE of original problem:
#include <iostream>
#include <map>
void read()
{
static std::string line;
std::getline(std::cin, line);
...
3
votes
2
answers
107
views
Forwarding vs not Forwarding a function passed to a wrapper
My question is what is the advantage of perfect forwarding a function, which is passed to our wrapper.
template<typename T, typename ...U>
auto time_function(T&& func, U&& ....
4
votes
2
answers
89
views
function objects for class methods
I know how to make a function an input argument when the function isn't a method, but I can't figure out how to do it when the function is a method.
Here's what I tried:
#include <iostream>
...
3
votes
1
answer
1k
views
C++ Functor Syntax
I have a question regarding the syntax of how to call a functor in C++. In the code below, why do the first 2 examples work whilst the 3rd and 4th attempt do not?
Also I can call ob (100); and it ...
2
votes
4
answers
194
views
Is function object created by `new` constructor treated as mutable object in javascript?
From study, I understood that in javascript, mutable objects() are treated by call-by-reference, and immutable objects are treated by call-by-value calling convention.
Let's say I use this kind of ...
0
votes
0
answers
172
views
C++: Why are function object base classes, adaptors and binders deprecated(C++11) and removed(C++17)? [duplicate]
Why are function object base classes (unary_function, binary_function), adaptors (pointer_to_unary_function, pointer_to_binary_function, ...) and binders(binder1st, binder2nd, ...) deprecated(C++11) ...
6
votes
1
answer
2k
views
Dart(/Flutter): Create function in initializer list
I am implementing a class with multiple constructors, which is internally build around a IndexedWidgetBuilder (a function object)
typedef IndexedWidgetBuilder = Widget Function(BuildContext context, ...
3
votes
0
answers
86
views
How can std::function accept all function objects with the same function call operator signature? [duplicate]
I am curious as to how std::function can accept objects of different types as long as they have the same function call operator signature. For instance, these two structs are clearly unrelated, and ...
4
votes
4
answers
961
views
C++ Passing function objects as lvalues and/or rvalues
I have a class that should filter its contents according to a user-supplied predicate. The interface I am given prescribes taking a reference to the predicate:
class Test {
vector<int> data;
...
3
votes
5
answers
81
views
What happens to variables/objects in inner classes of function objects?
I have a function multi2 which returns inner class Inner as an Object.
What happens to a - where is it saved and how can I access it?
public class C {
private static Object multi2(final int a) {
...
7
votes
1
answer
212
views
When using templates to support functor as arguments, what qualifier should I use?
Consider this code:
template<class F>
void foo1(F f) { f(); }
template<class F>
void foo2(F const& f) { f(); }
template<class F>
void foo3(F&& f) { f(); }
Which ...
1
vote
2
answers
335
views
Omitting type parameter for greater template in C++ STL
Here is a line of code I read in a book
priority_queue<IteratorCurrentAndEnd, vector<IteratorCurrentAndEnd>, greater<>> min_heap;
where IteratorCurrentAndEnd is a class ...
2
votes
3
answers
160
views
Why do I need pass a comparator to construct a priority_queue when it is a lambda, but not when it is std::greater?
I am reading a code sample from cppreference:
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
template<typename T> void print_queue(T& ...
4
votes
0
answers
69
views
Copy constructor is called many times when constructing a thread by function object [duplicate]
I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is ...
10
votes
3
answers
2k
views
Are function pointers function objects in C++?
The C++ standard defines function objects as:
A function object type is an object type that can be the type of the
postfix-expression in a function call. (link)
First I was thinking that ...
8
votes
5
answers
639
views
how to pass a class method as argument to another method of the class in perl 6
I have a script like the below. Intent is to have different filter methods to filter a list.
Here is the code.
2
3 class list_filter {
4 has @.my_list = (1..20);
5
6 method filter($l)...
2
votes
2
answers
1k
views
How to copy references of elements of one vector to another vector?
I have a vector:
std::vector<island> sea;
And now I want to hold pointers to all elements of this vector in another vector. But just for a personal exercise I want to do this in a fancy C++ ...