来源:https://stackoverflow.com/questions/1358427/function-which-returns-an-unknown-type
参考:http://cs.brown.edu/~jwicks/boost/doc/html/variant.html
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.