144 | | == Template and ambiguity == |
145 | | |
146 | | == Examples == |
| 144 | == Template == |
| 145 | Sometimes you might have to create know the template arguments of a Functor. This may be the case if the name of a function is ambiguous and createFunctor can't decide which function to use. |
| 146 | |
| 147 | This is how the template arguments are arranged: |
| 148 | {{{ |
| 149 | template <classname, returntype, paramtype1, ..., paramtype5> |
| 150 | }}} |
| 151 | |
| 152 | If a function has no returnvalue, the returntype argument is discarded: |
| 153 | {{{ |
| 154 | template <classname, paramtype1, ..., paramtype5> |
| 155 | }}} |
| 156 | |
| 157 | If a function is static, the classname argument is discarded: |
| 158 | {{{ |
| 159 | template <returntype, paramtype1, ..., paramtype5> |
| 160 | }}} |
| 161 | |
| 162 | If a function has less than 5 parameters, the surplus parameters are discarded: |
| 163 | {{{ |
| 164 | example: 3 parameters: |
| 165 | template <paramtype1, paramtype2, paramtype3> |
| 166 | }}} |
| 167 | |
| 168 | Examples: |
| 169 | {{{ |
| 170 | int function(float p1, float p2, float p3); |
| 171 | => <int, float, float, float> |
| 172 | |
| 173 | void function(const std::string& p); |
| 174 | => <const std::string&> |
| 175 | |
| 176 | void Class::function(int p); |
| 177 | => <Class, int> |
| 178 | |
| 179 | bool Class::function(int p); |
| 180 | => <Class, bool, int> |
| 181 | }}} |
| 182 | |
| 183 | == Ambiguity == |
| 184 | Sometimes two functions have the same name but different parameters. This is called overloading. Usually the compiler knows what you want because your arguments define the function. But if you just pass the function-pointer to createFunctor, the compiler has no idea which function to chose. |
| 185 | |
| 186 | Example: |
| 187 | {{{ |
| 188 | class SomeClass |
| 189 | { |
| 190 | void someFunction(const Vector3& position); |
| 191 | void someFunction(float x, float y, float z); |
| 192 | }; |
| 193 | |
| 194 | Functor* functor = createFunctor(&SomeClass::someFunction); // error! |
| 195 | }}} |
| 196 | |
| 197 | This problem can be solved by specifying the template arguments by yourself. Read the chapter above to learn about how to arrange the template arguments. |
| 198 | |
| 199 | This is how it works: |
| 200 | {{{ |
| 201 | class SomeClass |
| 202 | { |
| 203 | void someFunction(const Vector3& position); |
| 204 | void someFunction(float x, float y, float z); |
| 205 | }; |
| 206 | |
| 207 | Functor* functor1 = createFunctor<SomeClass, const Vector3&>(&SomeClass::someFunction); |
| 208 | Functor* functor2 = createFunctor<SomeClass, float, float, float>(&SomeClass::someFunction); |
| 209 | }}} |