0

I have 2 questions in my journey to understanding functors in C++.

I was reading the answer to this question and looked at the example and couldn't see the difference between functors and constructors with the exception of a having a return type.

  1. Would the functor's state and behavior be replicated with just the combination of a constructor and an instance method? The instance method would have a return type. The example already has a constructor hence the functor doesn't add much. Isn't the functor also an instance method?

  2. Wouldn't you just need auto to make it confusing to figure out if you are dealing with a function or constructor?

    add_x add42(42);

    // somewhere deeper in the code
    auto X = add42(8);
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33
  • Not sure what your confusion is here. From the linked post, all a functor is, is a class with an overload `operator()` – NathanOliver Mar 18 '20 at 12:40
  • 2
    This confusion about if dealing with a function or object construction is what leads to [the most vexing parse](https://en.wikipedia.org/wiki/Most_vexing_parse) and the main reason behind the introduction of [uniform initialization](https://www.geeksforgeeks.org/uniform-initialization-in-c/) in the C++11 standard. Good context-aware editors help to remove some confusion. – Some programmer dude Mar 18 '20 at 12:44
  • "*Wouldn't you just need auto to make it confusing to figure out if you are dealing with a function or constructor?*" Why would you care? Outside of the question of what type `X` is (which you ought to be able to determine from the context, or else you shouldn't have used `auto` to begin with), what does it matter if that's a constructor or an `operator()` call? – Nicol Bolas Mar 18 '20 at 13:25
  • @NicolBolas, I prefer not to use auto for this reason but I may be reading someone else's code. Now suppose this is a large codebase at a new job [not unreasonable given that people move companies more frequently now] and you have business and domain logic to learn as well - that's the job context (pun intended only if you think it is funny otherwise not intended). – heretoinfinity Mar 18 '20 at 14:15

1 Answers1

2

A functor is an object, with an overloaded call operator.

Constructor is a function that cannot be called directly. The compiler will generate a call to a constructor when an (non trivial) object is created.

add_x add42(42);

This is syntax for direct initialisation. add_x is a type, add42 is the name of a variable, and 42 is the parameter list to the constructor.

auto X = add42(8);

Because we know that add42 is a variable, we know that X is initialised with the result of the invokation of the function call operator.

If add42 was a type instead of an object, this could potentially instead be initialisation of that type.

Yes, the syntax of the call operator and the syntax of initialisation are the same. The context determines which is being used.

eerorika
  • 232,697
  • 12
  • 197
  • 326