如何泛化接口以要求某个元组的返回值

how to generalize an interface to require a return value of some tuple?

本文关键字:元组 返回值 何泛化 泛化 接口      更新时间:2023-10-16

我正在使用SOCI库,它对boost和std::tuple<>有很好的支持。

我的目的是定义一个名为SOCI_Streamable抽象接口,它需要一个返回一些元组的方法。

我让它适用于 gcc 4.7.2 中的特定元组,但我需要帮助抽象此接口以适用于任何元组

换句话说,我想将此要求转换为代码:一个类满足SOCI_Streamable的要求,如果它可以返回某种 std::tuple - 我不在乎哪种。

下面的代码进行重大更改是可以的,只要它满足要求。 我怀疑需要某种模板代码甚至 CRTP,但我不确定。

#include<tuple>
// want to generalize next line to any std::tuple<>
typedef std::tuple<int const&,char const> some_tuple;    
class SOCI_Streamable
{
public:
  virtual some_tuple obj_as_tuple() const = 0; 
};
class Foo :
    public SOCI_Streamable
{
public:
  virtual some_tuple obj_as_tuple() const 
    {
        return std::forward_as_tuple( m_int, m_char );
    }
private:
    int   m_int;
    char  m_char;
};
int main( int argc, char* argv[] )
{
}
template <class A,class B>
class SOCI_Streamable
{
public:
  typedef std::tuple<A,B> Tuple;
  virtual Tuple obj_as_tuple() const = 0; 
};
class Foo :
   public SOCI_Streamable<int const&,char const>
{

如果可以在实现者级别指定类型,这应该可以工作。但这确实将单个接口更改为一系列接口。让我看看是否有更好的方法.