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.
function-object
168
questions
8
votes
3
answers
529
views
What's the meaning of &T::operator() in function traits?
// Fallback, anything with an operator()
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> > {
};
What does T::operator() mean here?
...
1
vote
1
answer
77
views
how to save any callable in c++ template class
I wanted to define a scopeguard, which runs some clean up code when scope exit.
And I don't want to use std::function<void()> since it has some overhead. Then I come up with this implementation ...
-2
votes
2
answers
397
views
How to invoke any swift function conditionally (without if block)
I want to write a general-purpose Swift function that serves the following simple purpose:
Take any function as argument
Take a Bool argument
If the bool argument is TRUE, invoke the input function ...
0
votes
2
answers
172
views
Is there an objective reason why the explicitly instantiated std::less, std::greater and similar offer no conversion to function pointer?
Stateless lambdas can be converted to function pointers, e.g. this is valid,
using Fun = bool(*)(int, int);
constexpr auto less = [](int a, int b){ return a < b; };
Fun f{less};
but objects like ...
3
votes
1
answer
99
views
What is the equivalent of std::less for the three-way comparison operator?
Does C++20 define something similar to std::less for three-way comparison operator (<=>).
I would like define some datasructure with customizable comparator as map or unordered_map do.
template &...
1
vote
1
answer
214
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
81
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 ...
20
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
91
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 ...
3
votes
1
answer
863
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
114
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
341
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
144
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
150
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 ...
-1
votes
1
answer
237
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 ...