What is this programming technology? (Boost Library)

I am trying to understand an example from the program_options boost library ( http://www.boost.org/doc/libs/1_38_0/doc/html/program_options/tutorial.html#id3761458 )

Especially this part:

desc.add_options() ("help", "produce help message") ("compression", po::value<int>(), "set compression level") ; 

what exactly is he doing here, and what technique is this?

This part of desc.add_options () may be a function call, but how does the other () fit here? Is this some kind of operator overload?

Thanks!

+4
source share
2 answers

The add_options () function actually returns a functor , that is, an object that overrides the operator (). This means that the next function call

 desc.add_options() ("help", "produce help message"); 

really expanding to

 desc.add_options().operator()("help", "produce help message"); 

"operator ()" also returns a functor, so calls can be fettered as you showed.

+14
source

Supposedly add_options () returns some functor whose operator () is overloaded to support the chain (which is very useful, BTW)

Overloading (...) allows you to create a class that acts as a function.

For instance:

 struct func { int operator()(int x) { cout << x*x << endl; } }; ... func a; a(5); //should print 25 

However, if you make a statement (), return a reference to the object, then you can "cling" to the statements.

 struct func { func& operator()(int x) { cout << x*x << endl; return *this; } }; ... func a; a(5)(7)(8); //should print 25 49 64 on separate lines 

Since a (5) returns a, (a (5)) (7) is more or less identical to a(5); a(7); a(5); a(7); .

+11
source

All Articles