What is the merit of the "function" type (not "pointer to function")
        Posted  
        
            by 
                anatolyg
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by anatolyg
        
        
        
        Published on 2010-12-21T07:33:49Z
        Indexed on 
            2010/12/21
            7:54 UTC
        
        
        Read the original article
        Hit count: 304
        
Reading the C++ Standard, i see that there are "function" types and "pointer to function" types:
typedef int func(int);     // function
typedef int (*pfunc)(int); // pointer to function
typedef func* pfunc;       // same as above
I have never seen the function types used outside of examples (or maybe i didn't recognize their usage?). Some examples:
func increase, decrease;            // declares two functions
int increase(int), decrease(int);   // same as above
int increase(int x) {return x + 1;} // cannot use the typedef when defining functions
int decrease(int x) {return x - 1;} // cannot use the typedef when defining functions
struct mystruct
{
    func add, subtract, multiply;   // declares three member functions
    int member;
};
int mystruct::add(int x) {return x + member;} // cannot use the typedef
int mystruct::subtract(int x) {return x - member;}
int main()
{
    func k; // the syntax is correct but the variable k is useless!
    mystruct myobject;
    myobject.member = 4;
    cout << increase(5) << ' ' << decrease(5) << '\n'; // outputs 6 and 4
    cout << myobject.add(5) << ' ' << myobject.subtract(5) << '\n'; // 9 and 1
}
Seeing that the function types support syntax that doesn't appear in C (declaring member functions), i guess they are not just a part of C baggage that C++ has to support for backward compatibility.
So is there any use for function types, other than demonstrating some funky syntax?
© Stack Overflow or respective owner