使用多个模板参数包包装模板 class

Wrapper over a templated class with more than one template parameter pack

我正在为来自第三方库的 class 写一个薄包装。

我的代码如下所示:

template<typename TArg1, typename... TPack1, typename... TPack2>
class MyWrapper<TArg1, TPack1..., TPack2...> : protected ThirdParty<TArg1, TPack1..., TPack2...>
{
    // class is empty
};

我的包装器的模板参数模仿第三方的模板参数 class。

但是,我的代码无法编译。我得到的第一个编译器错误是:
error C3856: 'MyWrapper': symbol is not a class template

我认为这是我的代码没有为编译器提供足够的上下文来推导模板参数包的问题。因此,即使它们不在 ThirdParty 级别,它们在 MyWrapper 级别也是不明确的。我一直在努力理解 ThirdParty 如何保证它,但我到目前为止还没有(我什至不知道这是否是问题所在)。

对于完整的上下文,这个问题的具体情况是我试图从名为 entt 的 ECS 库中包装一个结构:entt::basic_view.

您没有声明 class template, but something like partial specilization。 正确的做法应该是

template <typename T>
class MyWrapper : protected ThirdParty<T>;

我不使用你的模板参数,因为它包含 2 个模板参数包。编译器不知道如何拆分模板参数。

所以我去看source code

template<typename, typename, typename, typename = void>
class basic_view;

template<typename Entity, typename... Component, typename... Exclude>
class basic_view<Entity, get_t<Component...>, exclude_t<Exclude...>> {
}; // get_t / exclude_t is a struct template type.

您可以在偏特化中使用多个模板参数包。

template <typename T, typename T, typename T, typename T = void>
class MyWrapper;  // without definition neither

template<typename Entity, typename... Component, typename... Exclude>
class MyWrapper<Entity, get_t<Component...>, exclude_t<Exclude...>>: protected basic_view<Entity, get_t<Component...>, exclude_t<Exclude...>> {
};

它工作正常,参见 demo