Forcing templates...
I think you need to post more representative code, or at least elaborate more than you have. The base case of a templated function parameterized on return type defined in namespace scope is compiling just fine in both MSVC 6 and gcc 3.2.
OK heres some code similar to whats going on.
class rec
{
//data. I say each variable has a number- a key
templateT getkey(unsigned int key);
};
template
T rec::getkey(unsigned int key)
{
/*
here a switch-case in which depending on the value of key, a different variable of possibly differing type is returned.
*/
}
int main()
{
//call getkey.
}
Is that enough code to get things rolling ?
~V''lion
Bugle4d
class rec
{
//data. I say each variable has a number- a key
templateT getkey(unsigned int key);
};
template
T rec::getkey(unsigned int key)
{
/*
here a switch-case in which depending on the value of key, a different variable of possibly differing type is returned.
*/
}
int main()
{
//call getkey.
}
Is that enough code to get things rolling ?
~V''lion
Bugle4d
No, it''s not enough.
The following compiles and runs fine on my system (although it complains about no return value)
int main()
{
//call getkey.
}
Please give an example which doesn''t compile but is exatcly what you want to achieve.
The following compiles and runs fine on my system (although it complains about no return value)
int main()
{
//call getkey.
}
Please give an example which doesn''t compile but is exatcly what you want to achieve.
I think that what you are trying to do is not possible. the return type is established at compile time, but you want to do it at runtime. What you will have to do is declare a base class that the function returns, with a set of virtual functions. Derive the required return types from this base class. This is assuming that you can give a common interface to all the types that you wish to return.
Think about what you are trying to do, if the function say returned an int, if you passed 0, and a float if you passed in 1, if you did not know the variable you were passing in (e.g. the user enters it), then how are you going to determine the variable to assign the return value to?
e.g. if var = 0 this is valid
int i = func(var);
but if var = 1, then the float would be converted to an int, not what you are expecting.
Think about what you are trying to do, if the function say returned an int, if you passed 0, and a float if you passed in 1, if you did not know the variable you were passing in (e.g. the user enters it), then how are you going to determine the variable to assign the return value to?
e.g. if var = 0 this is valid
int i = func(var);
but if var = 1, then the float would be converted to an int, not what you are expecting.
Say you have a function:
template<class T>
T func(int param)
{ //....
}
then, forcing template...
int temp = func<int> (0);
you can't...
int temp = func(0);
int temp = func((int)0);
... since solving the T needs at least a parameter of type T.
[edited by - DerekSaw on March 4, 2003 8:40:27 PM]
[edited by - DerekSaw on March 4, 2003 8:41:06 PM]
template<class T>
T func(int param)
{ //....
}
then, forcing template...
int temp = func<int> (0);
you can't...
int temp = func(0);
int temp = func((int)0);
... since solving the T needs at least a parameter of type T.
[edited by - DerekSaw on March 4, 2003 8:40:27 PM]
[edited by - DerekSaw on March 4, 2003 8:41:06 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement