对具有不同模板参数的列表项调用参数化方法

Calling parametrised method on list items with different template parameters

本文关键字:参数 列表 调用 方法      更新时间:2023-10-16

我正在尝试存储和操作具有不同参数类型的模板类对象列表;模板类有两个参数化方法,一个返回参数类型,一个 void 接受它作为输入。

更具体地说,我有一个模板类定义如下:

template<typename T>
class Test
{
public:
    virtual T a() = 0;
    virtual void b(T t) = 0;
};

以及它的不同规格,例如:

class TestInt : public Test<int>
{
public:
    int a() {
        return 1;
    }
    void b(int t) {
        std::cout << t << std::endl;
    }
};
class TestString : public Test<std::string>
{
public:
    std::string a() {
        return "test";
    }
    void b(std::string t) {
        std::cout << t << std::endl;
    }
};

我希望能够在一个列表中存储TestIntTestString类型的不同对象,并循环调用一种方法作为另一个方法的输入,如下所示:

for (auto it = list.begin(); it != list.end(); ++it)
    (*it)->b((*it)->a());

我已经研究了boost::any但无法将迭代器强制转换为特定类,因为我不知道每个存储对象的特定参数类型。也许这不能像C++那样在静态类型语言中完成,但我想知道是否有解决方法。

为了完整起见,我要补充一点,我的总体目标是开发一个"参数化观察者",即能够使用不同的参数定义一个观察者(如观察者模式):Test类是观察者类,而我试图正确定义的不同类型的观察器列表存储在主题类中, 通过两种方法通知他们 a()b() .

虚拟在这里实际上没有任何意义,因为对于每个T,签名都是不同的。

因此,您似乎拥有永恒的"我们如何模拟虚拟函数模板"或"如何创建没有虚拟函数的界面"的另一个版本:

  • 生成没有虚函数的接口?
  • 如何在C++实现"虚拟模板功能"

第一个基本上包含一个你可以在这里使用的想法。

以下是我会做什么的想法:

住在科里鲁

#include <algorithm>
#include <iostream>
namespace mytypes {
    template <typename T>
    struct Test {
        T a() const;
        void b(T t) { std::cout << t << std::endl; } 
    };
    template <> int         Test<int>::a()         const { return 1;      }
    template <> std::string Test<std::string>::a() const { return "test"; }
    using TestInt    = Test<int>;
    using TestString = Test<std::string>;
}
#include <boost/variant.hpp>
namespace mytypes { 
    using Value = boost::variant<int, std::string>;
    namespace detail {
        struct a_f : boost::static_visitor<Value> {
            template <typename T>
            Value operator()(Test<T> const& o) const { return o.a(); }
        };
        struct b_f : boost::static_visitor<> {
            template <typename T>
            void operator()(Test<T>& o, T const& v) const { o.b(v); }
            template <typename T, typename V>
            void operator()(Test<T>&, V const&) const {
                throw std::runtime_error(std::string("type mismatch: ") + __PRETTY_FUNCTION__);
            }
        };
    }
    template <typename O>
    Value a(O const& obj) {
        return boost::apply_visitor(detail::a_f{}, obj);
    }
    template <typename O, typename V>
    void b(O& obj, V const& v) {
        boost::apply_visitor(detail::b_f{}, obj, v);
    }
}
#include <vector>
int main()
{
    using namespace mytypes;
    using AnyTest = boost::variant<TestInt, TestString>;
    std::vector<AnyTest> list{TestInt(), TestString(), TestInt(), TestString()};
    for (auto it = list.begin(); it != list.end(); ++it)
        b(*it, a(*it));
}

这打印

1
test
1
test

积分

如果您坚持,您可以将 AnyTest 变体包装到一个适当的类中,并在其上a()b(...)成员函数:

住在科里鲁

int main()
{
    using namespace mytypes;
    std::vector<AnyTest> list{AnyTest(TestInt()), AnyTest(TestString()), AnyTest(TestInt()), AnyTest(TestString())};
    for (auto it = list.begin(); it != list.end(); ++it)
        it->b(it->a());
}

扩展我上面的评论,我目前能想到的实现您正在尝试做的事情的最简单的方法——至少正如我从您的示例代码中理解的那样——如下:

/* Interface for your container, better not forget the destructor! */
struct Test {
  virtual void operate(void) = 0;
  virtual ~Test() {}
};
/* Implementation hiding actual type */
template<typename T>
struct TestImpl : public T, public Test {
  void operate(void) {
    T::b(T::a());
  }
};
/* Actual code as template policies */
struct IntTest {
  int a(void) {
    return 42;
  }
  void b(int value) {
    std::cout << value << std::endl;
  }
};
struct StringTest {
  std::string a(void) {
    return "Life? Don't talk to me about life.";
  }
  void b(std::string value) {
    std::cout << value << std::endl;
  }
};

然后,您需要为类 Test 的对象创建一个容器,并用相应TestImpl<IntTest>TestImpl<StringTest> 等的对象填充它。为了避免对象切片,您需要引用或指针语义,例如std::vector<std::unique_ptr<Test> >

for (auto it = list.begin(); it != list.end(); ++it) {
  (*it)->operate();
}