Several modern languages provide a convenient way to refer the function parameters by name also when you call the function. It is especially comfortable in case of a function having tons of parameters with natural default values. Sadly, C++ lack this amenity.
However, with a crafty trick and with some little inconvenience, it is possible to emulate is. The example below shows how to do it.
Then you can use it like this.
The trick is obvious, each "named parameter" changes one component of the underlying class, then gives back a reference to it. Finally, run()
executes the algorithm itself.
NamedFn
..run()
could be made unnecessary, because the algorithm could also be implemented in the destructor of namedFn
instead. This however would make it impossible to implement functions with return values, and would also cause serious problems when implementing named template parameters. Therefore, by convention, .run()
must be used explicitly to execute a function having named parameters everywhere in LEMON.A named parameter can also be a template function. The usage is exactly the same, but the implementation behind is a kind of black magic and they are the dirtiest part of the LEMON code.
You will probably never need to know how it works, but if you really committed, have a look at lemon/graph_to_eps.h for an example.
A similar game can also be played when defining classes. In this case the type of the class attributes can be changed. Initially we have to define a special class called Traits Class defining the default type of the attributes. Then the types of these attributes can be changed in the same way as described in the next section.
See lemon::DijkstraDefaultTraits for an example how a traits class implementation looks like.
If we would like to change the type of an attribute in a class that was instantiated by using a traits class as a template parameter, and the class contains named parameters, we do not have to instantiate again the class with new traits class, but instead adaptor classes can be used as shown in the following example.
It can also be used in conjunction with other named template parameters in arbitrary order.
The result will be an instantiated Dijkstra class, in which the DistMap and the PredMap is modified.