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.
Questions tagged [function-object]
168 questions
                    
                    1061
                    
            votes
                
                14 answers
            
        What are C++ functors and their uses?
I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?
         
    
    
        Konrad
        
- 39,751
- 32
- 78
- 114
                    138
                    
            votes
                
                9 answers
            
        Build a function object with properties in TypeScript
I want to create a function object, which also has some properties held on it. For example in JavaScript I would do:
var f = function() { }
f.someValue = 3;
Now in TypeScript I can describe the type of this as:
var f: { (): any; someValue: number;…
         
    
    
        JL235
        
- 2,455
- 2
- 19
- 14
                    62
                    
            votes
                
                3 answers
            
        Are functors actually faster than pointers to functions?
According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, which increases speed.
I have two questions about this:
How can we verify…
         
    
    
        user7140484
        
- 920
- 6
- 14
                    57
                    
            votes
                
                5 answers
            
        How does the template parameter of std::function work? (implementation)
In Bjarne Stroustrup's home page (C++11 FAQ):
struct X { int foo(int); };
std::function f;
f = &X::foo; //pointer to member
X x;
int v = f(&x, 5); //call X::foo() for x with 5
How does it work? How does std::function call a foo… 
         
    
    
        Sadeq
        
- 7,795
- 4
- 34
- 45
                    40
                    
            votes
                
                2 answers
            
        Overloading multiple function objects by reference
In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject, returns a new function object that behaves like an overload of fs.... Example:
template 
struct… 
         
    
    
        Vittorio Romeo
        
- 90,666
- 33
- 258
- 416
                    28
                    
            votes
                
                7 answers
            
        How can it be useful to overload the "function call" operator?
I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so:
class A { 
  int n;
public: 
  void operator ()() const; 
};
And then use it this way:
A…
         
    
    
        lurks
        
- 2,576
- 4
- 30
- 39
                    23
                    
            votes
                
                4 answers
            
        Overload a lambda function
How to overload a simple local lambda function?
SSE of original problem:
#include 
#include  
         
    
    
        Vinícius
        
- 15,498
- 3
- 29
- 53
                    22
                    
            votes
                
                3 answers
            
        why function objects should be pass-by-value
I have just read the classic book "Effective C++, 3rd Edition", and in item 20 the author concludes that built-in types, STL iterators and function object types are more appropriate for pass-by-value. I could well understand the reason for built-in…
         
    
    
        JavaBeta
        
- 510
- 7
- 16
                    21
                    
            votes
                
                2 answers
            
        What does static_cast mean when it's followed by two pairs of parentheses?
What does this say:
return static_cast(*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 on, there's nothing inside the first pair of parentheses.
 
         
    
    
        johnsmith
        
- 515
- 5
- 12
                    20
                    
            votes
                
                4 answers
            
        In C++ what does it mean for a compiler to "inline" a function object?
In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them.
I'm a bit foggy on exactly what this means in this context... or any context I'm…
         
    
    
        Monte Hurd
        
- 4,349
- 5
- 30
- 35
                    18
                    
            votes
                
                1 answer
            
        Call operator with auto return type being chosen instead of constructor when using std::function
The following snippet:
#include 
struct X {
    X(std::function fn); // (1)
    X(double, double);                   // (2)
    template 
    auto operator()(T const& t) const {  // (3)
        return t.foo();
…   
         
    
    
        Holt
        
- 36,600
- 7
- 92
- 139
                    18
                    
            votes
                
                3 answers
            
        Serializing function objects
Is it possible to serialize and deserialize a std::function, a function object, or a closure in general in C++? How? Does C++11 facilitate this? Is there any library support available for such a task (e.g., in Boost)?
For example, suppose a C++…
         
    
    
        shaniaki
        
- 319
- 2
- 8
                    14
                    
            votes
                
                3 answers
            
        javascript class inherit from Function class
I like that in javascript, I can create a function, and then add further methods and attributes to that function
myInstance = function() {return 5}
myInstance.attr = 10
I would like to create a class to generate these objects. I assume I have to…
         
    
    
        dgreisen
        
- 483
- 5
- 9
                    14
                    
            votes
                
                3 answers
            
        Detailed difference between functor's call and function call?
The key reason this works is that for_each () doesn’t actually assume
  its third argument to be a function.
  It simply assumes that its third
  argument is something that can be
  called with an appropriate argument. A
  suitably defined object…
         
    
    
        Ramadheer Singh
        
- 4,124
- 4
- 32
- 44
                    12
                    
            votes
                
                3 answers
            
        Creating a function object from a string
Question: Is there a way to make a function object in python using strings?
Info: I'm working on a project which I store data in a sqlite3 server backend. nothing to crazy about that. a DAL class is very commonly done through code generation…
         
    
    
        Narcolapser
        
- 5,895
- 15
- 46
- 56