我正在為來自第三方庫的類撰寫一個瘦包裝器。
我的代碼如下所示:
template<typename TArg1, typename... TPack1, typename... TPack2>
class MyWrapper<TArg1, TPack1..., TPack2...> : protected ThirdParty<TArg1, TPack1..., TPack2...>
{
// class is empty
};
我的包裝器的模板引數模仿第三方類中的模板引數。
但是,我的代碼無法編譯。我得到的第一個編譯器錯誤是:
error C3856: 'MyWrapper': symbol is not a class template
我認為這是我的代碼沒有為編譯器提供足夠的背景關系來扣除模板引數包的問題。因此,MyWrapper即使不在級別上,它們在級別上也是模棱兩可的ThirdParty。我一直試圖了解如何ThirdParty保證它,但到目前為止我還沒有(我什至不知道這是否是問題所在)。
對于完整的背景關系,這個問題的具體情況是我試圖從一個名為entt :的 ECS 庫中包裝一個結構entt::basic_view。
uj5u.com熱心網友回復:
您沒有宣告類模板,而是宣告了部分規范。正確的方法應該是
template <typename T>
class MyWrapper : protected ThirdParty<T>;
我不使用您的模板引數,因為它包含 2 個模板引數包。編譯器不知道如何拆分模板引數。
所以我去看看源代碼。
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...>> {
};
它作業正常,請參閱演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433974.html
上一篇:如何擴展初始化串列引數包?
