【发布时间】:2019-11-10 05:55:18
【问题描述】:
所以我有以下(减少的)课程:
template <typename A, typename... B>
struct ComponentTupleAccessor:
public ComponentArray<A>,
public ComponentTupleAccessor<B...>
{
ComponentTupleAccessor(const uint32_t capacity):
ComponentArray<A>(capacity),
ComponentTupleAccessor<B...>(capacity)
{}
};
template <typename A>
struct ComponentTupleAccessor<A>:
public ComponentArray<A>
{
ComponentTupleAccessor<A>(const uint32_t capacity):
ComponentArray<A>(capacity)
{}
};
template <typename A, typename ...B>
class ComponentTuple {
ComponentTupleAccessor<A, B...> m_Components;
uint32_t m_Capacity;
public:
ComponentTuple(const RB32u capacity):
m_Capacity{capacity},
m_Components(capacity)
{}
template <typename S, typename ...T>
void pop_back() {
m_Components.Component<S>::pop_back();
pop_back<T...>();
}
template <typename S>
void pop_back() {
m_Components.Component<S>::pop_back();
}
void pop_back() {
pop_back<A, B...>();
}
};
ComponentArray 类基本上是一个包含一组特定类型组件的向量的包装器。
ComponentBlockTupleAccessor 类或多或少模拟了 std::tuple 的缩减版本,其中可以使用可变参数模板将任意数量的 ComponentArray 唯一类型继承到 ComponentTuple 类中。
ComponentTuple 中的pop_back 函数旨在递归地pop_back 一个元素从每个ComponentArrays 中分离出来。
在ComponentTuple 类之外,我希望能够简单地调用compTupleInstance.pop_back() 之类的东西,并且所有ComponentArray 都应该删除它们的最后一个元素。
我得到一个编译错误“重载‘pop_back()’的调用不明确”pop_back();
我似乎无法找出 A、B(包)、S 和 T(包)模板参数的组合来提供我需要的功能。我在这里错过了什么?
编辑:这是一个简单的使用场景:
// ComponentTuple contains an int and a float ComponentArray with capacity 8.
ComponentTuple<int, float> dut(8);
// Push a set of new components to the ComponentArrays.
// This function has a similar structure to that of pop_back.
dut.push_back({8}, {3.141f});
// Another one
dut.push_back({4}, {2.718f});
// Remove the last element from all of the ComponentArrays.
dut.pop_back();
ComponentTuple 模板参数总是唯一的类型,并且总是大于一。
【问题讨论】:
-
如果 T 参数包为空,则有 2 个相同的 push_back()
-
我认为问题在于您的
pop_back函数不接受任何参数;这意味着它们最终具有相同的签名。也许您可以详细说明您的目标是什么功能。添加一些代码来显示您打算如何调用这些方法? -
@Willem 是的,你是对的。我有一个类似结构的 push_back 函数,它没有任何问题,因为函数签名总是明确的。我如何在没有至少一个非可变参数的情况下维护递归模板功能?
标签: c++ variadic-templates recursive-datastructures recursive-templates