I
I
iasonov2011-11-13 01:40:03
C++ / C#
iasonov, 2011-11-13 01:40:03

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:

  • decltype
  • auto

Answer the question

In order to leave comments, you need to log in

8 answer(s)
M
megalol, 2011-11-13
@iasonov

#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;
}

D
Dzuba, 2011-11-13
@Dzuba

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.

C
CorporateShark, 2011-11-13
@CorporateShark

#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;
}

Will it fit?

A
Akson87, 2011-11-13
@Akson87

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.

Y
Yaraife, 2011-11-13
@Yaraife

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.

A
agmt, 2011-11-13
@agmt

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?

A
Adam_Ether, 2011-11-13
@Adam_Ether

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.

B
burdakovd, 2011-11-13
@burdakovd

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 question

Ask a Question

731 491 924 answers to any question