我想从函数返回任意类型。返回类型动态地取决于多态函数调用。例如,以下从具有不同列数的 CSV 文件中读取值,
struct File
{
virtual SOMETHING getTuple();
};
struct File1 : File
{
SOMETHING getTuple()
{
return std::tuple<int, double, string>();
}
};
struct File2 : File
{
SOMETHING getTuple()
{
return std::tuple<string, string>();
}
};
int main()
{
std::ifstream fs("file.csv");
File2 f;
BOOST_AUTO(tuple, f.getTuple())
BOOST_AUTO(row, read(fs,tuple));
BOOST_FOREACH(BOOST_TYPEOF(tuple) element , row )
{
for_each(element,print());
std::cout << std::endl;
}
};
我如何编写getTuple()
签名以根据调用的对象返回不同的类型?
解决方案:
class File1
{
std::ifstream ifs;
public:
typedef boost::fusion::vector<int, double, std::string> tuple;
File1() : ifs("file1.csv") {}
std::ifstream& getfs()
{
return ifs;
}
};
class File2
{
std::ifstream ifs;
public:
typedef boost::fusion::vector<std::string, std::string> tuple;
File2() : ifs("file2.csv") {}
std::ifstream& getfs()
{
return ifs;
}
};
int main()
{
File2 f;
typedef File2::tuple tuple;
BOOST_AUTO(x, read(f.getfs(),tuple()));
BOOST_FOREACH(tuple e , x )
{
for_each(e,print());
std::cout << std::endl;
}
}