|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I have a "genertic dictionary type" object (MyDictionary) that provides
an accessor method that has the following general signature: ReturnType getItem(TypeA row_specifier, TypeB col_specifier); Where: TypeA: {integer, char*} TypeB: {integer, char*} ReturnType: {integer, char*, double, bool, MyDictionary*} What is the best way to implement this, other than writing seperate functions for each possible combination of row_specifier, col_specifier and return type? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Jun 27, 1:11pm, Annonymous Coward <m...@home.com> wrote:
> I have a "genertic dictionary type" object (MyDictionary) that provides > an accessor method that has the following general signature: > > ReturnType getItem(TypeA row_specifier, TypeB col_specifier); > > Where: > > TypeA: {integer, char*} > TypeB: {integer, char*} > ReturnType: {integer, char*, double, bool, MyDictionary*} > > What is the best way to implement this, other than writing seperate > functions for each possible combination of row_specifier, col_specifier > and return type? First of all, is the logic and code for each combination of types very similar? If not, you will end up writing a lot of specializations anyway, in which case it may simply be easier to use separate overloaded functions. You cannot get overload and template type inference on return types - One way to get this is to make the return argument an out patameter - like this : template<typename TypeA,typename TypeB,typename ReturnType> getItem(TypeA row_specifier, TypeB col_specifier, ReturnType &ret) { // Whatever } // specialize as needed that way you can call it easily like this and avoid specifying the return type remplate parameter double r1; MyDictionary *r2; getItem(1, 2, r1); // args int, int , return double getItem("Foo", 7, r2); // args char*, int , return MyDictionary* Vivek |
|
![]() |
| Outils de la discussion | |
|
|