对具有不同模板参数的列表项调用参数化方法
Calling parametrised method on list items with different template parameters
我正在尝试存储和操作具有不同参数类型的模板类对象列表;模板类有两个参数化方法,一个返回参数类型,一个 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;
}
};
我希望能够在一个列表中存储TestInt
和TestString
类型的不同对象,并循环调用一种方法作为另一个方法的输入,如下所示:
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();
}
- 概念中的cv限定符需要表达式参数列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 错误 没有与参数列表匹配的重载函数"getline"实例
- std::vector 没有重载函数的实例与参数列表匹配
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 构造函数/函数声明参数列表中的统一初始化
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- C++-将具有引用的长参数列表重构为结构
- 缺少别名模板C++参数列表
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- "extern"声明以及带有和不带参数列表的类模板实例的后续定义
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 我可以使用宏自动构建参数列表吗?
- 类成员函数参数列表是否可以依赖于模板参数?