Function which returns an unknown type
If you know type at compile time you could use templates. If type depends on run-time, then using templates is not an option.
class Test
{
template<int> struct Int2Type {};
template<> struct Int2Type<1> { typedef int value_type; };
template<> struct Int2Type<2> { typedef float value_type; };
template<> struct Int2Type<3> { typedef char value_type; };
public:
template<int x> typename Int2Type<x>::value_type DoIt() {}; // error if unknown type used
template<> typename Int2Type<1>::value_type DoIt<1>() { return 2; };
template<> typename Int2Type<2>::value_type DoIt<2>() { return 1.2f; };
template<> typename Int2Type<3>::value_type DoIt<3>() { return 'a'; };
};
int main()
{
Test obj;
cout << obj.DoIt<2>();
return 0;
}
You can use boost::any
or boost::variant
to do what you want. I recommend boost::variant
because you know the collection of types you want to return.
This is a very simple example, though you can do much more with variant
. Check the reference for more examples :)
#include "boost/variant.hpp"
#include <iostream>
typedef boost::variant<char, int, double> myvariant;
myvariant fun(int value)
{
if(value == 0)
{
return 1001;
}
else if(value == 1)
{
return 3.2;
}
return 'V';
}
int main()
{
myvariant v = fun(0);
std::cout << v << std::endl;
v = fun(1);
std::cout << v << std::endl;
v = fun(54151);
std::cout << v << std::endl;
}
The output:
1001
3.2
V
I would use boost::variant
instead of a union
because you can't use non-POD types inside union
. Also, boost::any
is great if you don't know the type you are dealing with. Otherwise, I would use boost::variant
because it is much more efficient and safer.
Answering the edited question: If you don't want to ship Boost
with your code, take a look at bcp
. The description of bcp
from the same link:
The bcp utility is a tool for extracting subsets of Boost, it's useful for Boost authors who want to distribute their library separately from Boost, and for Boost users who want to distribute a subset of Boost with their application.
bcp can also report on which parts of Boost your code is dependent on, and what licences are used by those dependencies.
C++ is a strongly-typed language, and has no concept of an unknown type. You could try using boost::any, which can (sort of) specify any type. I would question the design of your function, however.