What is the merit of the "function" type (not "pointer to function")
- by anatolyg
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?