Generic Parameter

Hello EveryOne,
I have a problem, I must do one function that received a "generic" parameter and after doing some things, return the same type of value.

For example:
type MyFunction(type parameter1)
{
......
return a;
}
.......

struct myStruct1 struct myStruct2
{ {
int a; bool a;
int b; bool b;
} }

myStruct1 var1;
myStruct2 var2;
myStruct1 var3;
myStruct2 var4;
......
var1 =MyFunction (var3);
var2=MyFunction (var4)

How can I implement this??
Thanks a lot
[776 byte] By [demian666] at [2007-11-18 15:09:06]
# 1 Re: Generic Parameter
If you need to handle 'any' type of parameter... Then you need to look at a templatefunction.
OReubens at 2007-11-11 2:00:25 >
# 2 Re: Generic Parameter
this is exactly what I don't want to do.
The number of types for the parameter is indefined.
User may make new types, or change the types.
(the struct can be modify outside my proyect)
I must pass a "generic" type parameter
and ruturn the same "generic" type value.
And it must support the operator =
It isn't so simple
demian666 at 2007-11-11 2:01:25 >
# 3 Re: Generic Parameter
If you don't need to understand the data, then just handle it as opaque data. The assignment operator works for this, but you can't do indirection of course.

Is that what you want?
j0nas at 2007-11-11 2:02:21 >
# 4 Re: Generic Parameter
could you attach an example, please??
In the function I must make a "type cast" to return exactly the kind of type that I put on the parameter.

For example:

int a;
int b;
bool c;
bool d;

a=0;
b=MyFunction(a);
c=true;
d=myFunction(c);
------
void * MyFunction ( void * parameter)
{
.........

return out;
}
------
But it must receive any kind of classes. for example CString.
demian666 at 2007-11-11 2:03:27 >
# 5 Re: Generic Parameter
No, I don't get what you're after. Sure can use a "void *" for carring opaque data, but is that what you really want?

void *YourFunc1(void *someOpaqueData)
{
void *tmp;
...
/* You can copy the data (the pointer), but you can't do indirection on it */
tmp = someOpaqueData;
...
return someOpaqueData;
}
j0nas at 2007-11-11 2:04:20 >
# 6 Re: Generic Parameter
Originally posted by demian666
this is exactly what I don't want to do.
The number of types for the parameter is indefined.
User may make new types, or change the types.
(the struct can be modify outside my proyect)
I must pass a "generic" type parameter
and ruturn the same "generic" type value.
And it must support the operator =
It isn't so simple I'm not sure what you are really saying.. If the 'struct' may be modified outside of your control, how can you write a function to manipulate it (other than simply making a copy of it) since you will have no idea of what it contains, and therefore, have no idea of any property or value types within that struct to examine or update??
gjs368 at 2007-11-11 2:05:30 >
# 7 Re: Generic Parameter
It isn't a problem not to know the diferents fields of the struct,
in my function only need to know the type of the parameter I receive, and how to return a value of this type.
(sorry, but my english is a little fogotten, I'm Spanish).

Think this, if a call the function with a integer in the parameter, I must Return a interger ( I must call the function with a generic type that acepts a integer, and return I supose the same generic type that acepts the operator =)
Inside the function the type doesn't matter.
Thanks a lot
demian666 at 2007-11-11 2:06:26 >
# 8 Re: Generic Parameter
Originally posted by demian666
this is exactly what I don't want to do.
The number of types for the parameter is indefined.
User may make new types, or change the types.
What exactly do you mean by "user"? The programmer using your library? If so, then I don't see why a template isn't appropriate. For example, when someone uses vector<>, the vector<> doesn't know the type, and it allows me to make up new types and use in the vector<> class. I don't quite understand what you are trying to do here.I must pass a "generic" type parameter
and ruturn the same "generic" type value.There is no such thing as a "generic" type, except for void pointers. There are generic functions, also known as templates. And again, this is what templates do already.
And it must support the operator =The compiler will output an error if you are performing assignment on a type that is unassignable.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 2:07:29 >
# 9 Re: Generic Parameter
Originally posted by demian666
It isn't a problem not to know the diferents fields of the struct,
in my function only need to know the type of the parameter I receive, and how to return a value of this type.

template <typename T>
T SomeFunc( T& thisT )
{
T AValue;
AValue = thisT;
//...
return AValue;
}

Think this, if a call the function with a integer in the parameter, I must Return a intergerWhich is what the function I wrote does. It also will cause a compiler error if I try to use assignment on an unassignable type.
( I must call the function with a genericAgain, what is a "generic"? When you make a make a function call, you are either passing a known type, either built-in or user-defined or you are passing a void*. If you pass a void*, then you don't know what's being passed to you unless you have told the user that you can only pass certain types (but then, you would need another parameter to determine the type that has been passed if you pass a void*).

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 2:08:27 >
# 10 Re: Generic Parameter
I will try the two ways, with void * and with templates, but I'm not sure It will work
If it works, I'll post the code.
Thanks a lot
demian666 at 2007-11-11 2:09:30 >
# 11 Re: Generic Parameter
Originally posted by Paul McKenzie

template <typename T>
T SomeFunc( T& thisT )
{
T AValue;
AValue = thisT;
//...
return AValue;
}

Which is what the function I wrote does. It also will cause a compiler error if I try to use assignment on an unassignable type.
Again, what is a "generic"? When you make a make a function call, you are either passing a known type, either built-in or user-defined or you are passing a void*. If you pass a void*, then you don't know what's being passed to you unless you have told the user that you can only pass certain types (but then, you would need another parameter to determine the type that has been passed if you pass a void*).

Regards,

Paul McKenzie
demian666 at 2007-11-11 2:10:28 >
# 12 Re: Generic Parameter
I'm trying to do the code you tell me, but It appears errors.
Where is the problem.
Thanks
-------FUNCTION CALL----
FuncionReflexiva<int> a;
int b;
int c;
b=0;
c=6;
b=a.DevuelveInverso(c);
-------CLASS DEFINITION-----
template <typename T> class FuncionReflexiva
{
public:
FuncionReflexiva(void);
~FuncionReflexiva(void);
T DevuelveInverso( T& thisT );
};
-------CLASS IMPLEMENTATION
#include "funcionreflexiva.h"

template<typename T>
FuncionReflexiva<T>::FuncionReflexiva(void)
{
}
template<typename T>
FuncionReflexiva<T>::~FuncionReflexiva(void)
{
}
template<typename T>
T FuncionReflexiva<T>::DevuelveInverso( T& thisT )
{
T AValue;
AValue = thisT;

return AValue;
}
-----------
ERRORS:
Reflexion error LNK2019: unresolved external symbol "public: __thiscall FuncionReflexiva<int>::~FuncionReflexiva<int>(void)" (??1?$FuncionReflexiva@H@@QAE@XZ) referenced in function _main
Reflexion error LNK2019: unresolved external symbol "public: __thiscall FuncionReflexiva<int>::FuncionReflexiva<int>(void)" (??0?$FuncionReflexiva@H@@QAE@XZ) referenced in function _main
Reflexion error LNK2019: unresolved external symbol "public: int __thiscall FuncionReflexiva<int>::DevuelveInverso(int &)" (?DevuelveInverso@?$FuncionReflexiva@H@@QAEHAAH@Z) referenced in function _main
Reflexion fatal error LNK1120: 3 unresolved externals
demian666 at 2007-11-11 2:11:31 >
# 13 Re: Generic Parameter
Don't reinvent the wheel use Boost::any (see sig)
Improving at 2007-11-11 2:12:36 >
# 14 Re: Generic Parameter
Please be more explicit
demian666 at 2007-11-11 2:13:33 >
# 15 Re: Generic Parameter
Read the docs. You will see it could be just what your after. ( http://www.boost.org/doc/html/any.html)
Improving at 2007-11-11 2:14:37 >
# 16 Re: Generic Parameter
Originally posted by demian666
I'm trying to do the code you tell me, but It appears errors.
Where is the problem.
Thanks

This has been answered many times on dev-archive, it is even in the FAQ.

Template code must all be in the header file. When you create a template, the template declaration and definitions must appear in the same module.

// FuncionReflexiva.h
#ifndef FUNCION
#define FUNCION

template <typename T> class FuncionReflexiva
{
public:
FuncionReflexiva()
{
}

~FuncionReflexiva()
{
}

T DevuelveInverso( T& thisT )
{
T AValue;
AValue = thisT;
return AValue;
}
};
#endif

OR you do this if you want to separate the definition from the declaration:

#ifndef FUNCION
#define FUNCION
template <typename T> class FuncionReflexiva
{
public:
FuncionReflexiva();
~FuncionReflexiva();
T DevuelveInverso( T& thisT );
};

#include "funcionReflexiva.cpp
#endif

And remove the #include "funcionreflexiva.h" from the funcionreflexiva.cpp file. Also, remove funcionreflexiva.cpp from your project workspace if you have included it in your project workspace.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 2:15:33 >