Answer the question
In order to leave comments, you need to log in
C++. Does the return type of a function depend on the value of its parameter?
Hello.
Is it possible to make a function that will return different types depending on the value of one of the parameters?
If so, how?
An explanatory example: a function with one int parameter should return a double value if the parameter == 1, or a bool value if the parameter == 2.
Googling, I found something on the topic, but not that:
Answer the question
In order to leave comments, you need to log in
#include "boost/variant.hpp"
boost::variant<int, std::string > f(int i)
{
if (i == 0)
{
return 1;
}
else
{
return std::string("str");
}
}
int main()
{
std::cout << f(0) << " " << f(1) << std::endl;
}
It is forbidden.
Cause: The return type is determined at compile time, not at run time. Therefore: cannot depend on the value of a method parameter.
#include <iostream>
template <int I> struct i
{
enum { value = I };
};
double Func( i<1> P )
{
return 3.14159;
}
<code>
bool Func( i<2> P )
{
return false;
}
template <int I> std::string Func( i<I> P )
{
return "Something else";
}
int main()
{
std::cout << Func( i<1>() );
std::cout << std::endl;
std::cout << Func( i<2>() );
std::cout << std::endl;
std::cout << Func( i<3>() );
return 0;
}
Return a pointer to your value, and let the calling code think for itself what is in it and do not forget to clean up the memory, but this is not the best approach.
The best option in your case: return a value to a void pointer, after which the receiving function will already interact with it as with an object of the type you need.
void* f(int i){
int array[i];
return (void*)array;
}
int main(){
int * array2 = (int*)f(5);
return 0;
}
But in general it is bad. Too bad :)
Types were invented just for such situations.
Of course, you can work with the data, choosing the type you want, but this increases the chance of error.
Anything can be done in C++. That's just asking this question, it seems to me, he himself did not fully understand what he needed (no offense). You can shove anything in eax before returning. There are no types at its level. That's just what will happen at the place of the function call? How will he figure out what was returned to him?
And without context, it is also not clear what this is for.
PS and I can offer to return a string that will be XML, for example =) In this case, the decision on the type will indeed be made at runtime.
PPS I wonder why no one suggested union?
Maybe you can, but you don't have to.
Think about how reloading a function or using default parameters can be used in your case. And perhaps the question will disappear by itself.
Decide whether the caller will know at compile time what type of object it needs to get?
If so, then make the switch parameter a template parameter, as suggested
If not, then you can make types A and B implement the common interface I. In the interface, describe all the methods that the caller requires from these types.
And return a pointer to I from the function.
Type
class I {
// interface
// ...
virtual ~I() {}
};
class A: public I { ... }
class B: public I { ... }
I* foo(const int sw) {
return sw == 0 ? new A(...) : new B(...);
}
Of course, you can return not a raw pointer, but unique_ptr
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question