so I wrote something like that:
class MyClass
{
enum varType {INTEGER, DOUBLE, VECTOR};
int beautiful_integer;
double awesome_double;
std::vector<float> many_floats;
template <class T>
T getVariable(varType type)
{
if(type == INTEGER)
{
return beatiful_integer;
}
if(type == DOUBLE)
{
return awesome_double;
}
if(type == VECTOR)
{
return many_floats;
}
}
...
};
But my compiler throws error "In instantiation of ..." and basically tells me that the return types don't match (and lists all of the unmatched ones, except the right one) and then tries to instantiate it with another type (for example double) and tells me that the return type doesn't match with int and vector of floats.
What am I doing wrong and how to properly write a template function in order to return different types depeneding on the parameter it was called with. For example when I call:
MyClass some_class(); //EDIT: this should be MyClass some_class;
//thanks for pointing it out
int some_number = some_class.getVariable(INTEGER);
I want to assign the value of beautiful_integer to some_number
As alternative, with std:
template <class T>
const T& getVariable() const
{
return std::get<const T&>(std::tie(beautiful_integer, awesome_double, many_floats));
}
template <class T>
T& getVariable()
{
return std::get<T&>(std::tie(beautiful_integer, awesome_double, many_floats));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With