1 /* -*- C++ -*- |
1 /* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 * |
2 * |
3 * This file is a part of LEMON, a generic C++ optimization library |
3 * This file is a part of LEMON, a generic C++ optimization library. |
4 * |
4 * |
5 * Copyright (C) 2003-2008 |
5 * Copyright (C) 2003-2008 |
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 * (Egervary Research Group on Combinatorial Optimization, EGRES). |
7 * (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 * |
8 * |
23 \section named-func-param Named Function Parameters |
23 \section named-func-param Named Function Parameters |
24 |
24 |
25 Several modern languages provide a convenient way to refer the |
25 Several modern languages provide a convenient way to refer the |
26 function parameters by name also when you call the function. It is |
26 function parameters by name also when you call the function. It is |
27 especially comfortable in case of a function having tons of parameters |
27 especially comfortable in case of a function having tons of parameters |
28 with natural default values. Sadly, C++ lack this amenity. |
28 with natural default values. Sadly, C++ lack this amenity. |
29 |
29 |
30 However, with a crafty trick and with some little |
30 However, with a crafty trick and with some little |
31 inconvenience, it is possible to emulate is. |
31 inconvenience, it is possible to emulate is. |
32 The example below shows how to do it. |
32 The example below shows how to do it. |
33 |
33 |
34 \code |
34 \code |
35 class namedFn |
35 class namedFn |
36 { |
36 { |
37 int _id; |
37 int _id; |
38 double _val; |
38 double _val; |
39 int _dim; |
39 int _dim; |
40 |
40 |
41 public: |
41 public: |
42 namedFn() : _id(0), _val(1), _dim(2) {} |
42 namedFn() : _id(0), _val(1), _dim(2) {} |
43 namedFn& id(int p) { _id = p ; return *this; } |
43 namedFn& id(int p) { _id = p ; return *this; } |
44 namedFn& val(double p) { _val = p ; return *this; } |
44 namedFn& val(double p) { _val = p ; return *this; } |
45 namedFn& dim(int p) { _dim = p ; return *this; } |
45 namedFn& dim(int p) { _dim = p ; return *this; } |
46 |
46 |
47 run() { |
47 run() { |
48 std::cout << "Here comes the function itself\n" << |
48 std::cout << "Here comes the function itself\n" << |
49 << "With parameters " |
49 << "With parameters " |
50 << _id << ", " << _val << ", " << _dim << std::endl; |
50 << _id << ", " << _val << ", " << _dim << std::endl; |
51 } |
51 } |
52 }; |
52 }; |
53 \endcode |
53 \endcode |
54 |
54 |
55 Then you can use it like this. |
55 Then you can use it like this. |
74 explicitly to execute a function having named parameters |
74 explicitly to execute a function having named parameters |
75 everywhere in LEMON.</b> |
75 everywhere in LEMON.</b> |
76 |
76 |
77 \section named-templ-func-param Named Function Template Parameters |
77 \section named-templ-func-param Named Function Template Parameters |
78 |
78 |
79 A named parameter can also be a template functions. The usage is |
79 A named parameter can also be a template function. The usage is |
80 exactly the same, but the implementation behind is a kind of black |
80 exactly the same, but the implementation behind is a kind of black |
81 magic and they are the dirtiest part of the LEMON code. |
81 magic and they are the dirtiest part of the LEMON code. |
82 |
82 |
83 You will probably never need to know how it works, but if you really |
83 You will probably never need to know how it works, but if you really |
84 committed, have a look at \ref lemon/graph_to_eps.h for an example. |
84 committed, have a look at \ref lemon/graph_to_eps.h for an example. |
101 the class contains named parameters, we do not have to instantiate again |
101 the class contains named parameters, we do not have to instantiate again |
102 the class with new traits class, but instead adaptor classes can |
102 the class with new traits class, but instead adaptor classes can |
103 be used as shown in the following example. |
103 be used as shown in the following example. |
104 |
104 |
105 \code |
105 \code |
106 Dijkstra<>::SetPredNodeMap<NullMap<Node,Node> >::Create |
106 Dijkstra<>::SetPredMap<NullMap<Node,Arc> >::Create |
107 \endcode |
107 \endcode |
108 |
108 |
109 It can also be used in conjunction with other named template |
109 It can also be used in conjunction with other named template |
110 parameters in arbitrary order. |
110 parameters in arbitrary order. |
111 |
111 |
112 \code |
112 \code |
113 Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Edge> >::Create |
113 Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Arc> >::Create |
114 \endcode |
114 \endcode |
115 |
115 |
116 The result will be an instantiated Dijkstra class, in which the |
116 The result will be an instantiated Dijkstra class, in which the |
117 DistMap and the PredMap is modified. |
117 DistMap and the PredMap is modified. |
118 |
118 |