从函数中返回任意类型

时间:2013-04-19 11:54:59

标签: c++ templates boost boost-fusion boost-mpi

我想从函数中返回任意类型。返回类型动态依赖于多态函数调用。例如,以下内容从具有不同列数的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;
    }
}

1 个答案:

答案 0 :(得分:0)

你无法做到这一点。函数签名需要在编译时确定,因此它不能依赖于对象的动态类型。

您最接近的方法是使用CRTP进行静态多态。也就是说,执行以下操作:

template<class Derived>
struct FileBase 
{
    typename Derived::TupleType getTuple()
    {
        return typename Derived::TupleType();
    }
};


struct File1 : FileBase<File1>
{
   typedef std::tuple<int, double, string> TupleType;
};

struct File2 : FileBase<File2>
{
    typedef std::tuple<string, string>() TupleType; 
};

这将给出您在给定示例中所需的行为,但它不会通过运行时多态性起作用。