-4

Could you please explain following statements with example

Statement1

Ordinary functions have different types only when their signatures differ. However, function objects can have different types when their signatures are the same. In fact, each functional behavior defined by a function object has its own type. This is a significant improvement for generic programming using templates because you can pass functional behavior as a template parameter

Community
  • 1
  • 1
rajen v
  • 11

2 Answers2

5

Ordinary functions have different types only when their signatures differ.

Meaning that these functions both have the same type int(int):

int f1(int);
int f2(int);

while this has a different type void(int)

void f3(int);

However, function objects can have different types when their signatures are the same.

Meaning that these two classes are different types (as different classes always are):

class c1 {int operator()(int);};
class c2 {int operator()(int);};

In fact, each functional behavior defined by a function object has its own type.

I don't know exactly what the author means by "functional behaviour", but I think this is just restating that two class types are separate types.

This is a significant improvement for generic programming using templates because you can pass functional behavior as a template parameter

Meaning that you can specify a function class as a template type parameter:

temp<c1> thing_using_c1;
temp<c2> thing_using_c2;

But you can't specify a plain function that way, you'd need to supply the function some other way:

temp<int(int)> thing_using_f1(f1);
temp<int(int)> thing_using_f2(f2);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
-1

different classes can have different functions with the same name and its parameters. but in general non class functions thats not possible

Abhishek Ranjan
  • 238
  • 2
  • 14