首页 > 解决方案 > 给模板参数包起别名

问题描述

我想为参数包使用 using 别名,以便可以在代码库的其他地方使用模板。在下面的代码中,我注释了我将使用这些类型的行:

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
// this would be needed later
//    using UsableOtherArgs... = OtherArgs...;
    virtual OtherClass method(OtherArgs... a) = 0
};

template <typename DerivedOfBaseInstantiation>
struct CallerStructure {
// to be used here
//    OtherClass unknownCall(typename DerivedOfBaseInstantiation::UsableOtherArgs... args) {
//        DerivedOfBaseInstantiation instance;
//        return instance.method(args...);
//    }
}

在 的代码编写时CallerStructure, 的参数unknownCall是未知的,并且由CallerStructurewhereDerivedOfBaseInstantiation是派生自 的类型的实例化确定Base。在一个看起来像这样的更完整的示例中:

class OtherClass {
};

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
    using UsableOtherArgs... = OtherArgs...;

    virtual OtherClass method(OtherArgs... a) = 0;

};

struct Derived_df : Base<int, double, float> {
    OtherClass someMethod(Base::UsableFirstArg);  // int
    OtherClass method(double, float) override ;
};



template <typename DerivedOfBaseInstantiation>
struct CallerStructure {
    OtherClass knownCall(typename DerivedOfBaseInstantiation::UsableFirstArg a) {
        DerivedOfBaseInstantiation instance;
        return instance.someMethod(a);
    }
    OtherClass unknownCall(typename DerivedOfBaseInstantiation::UsableOtherArgs... args) {
        DerivedOfBaseInstantiation instance;
        return instance.method(args...);
    }
};


void instantiations() {
    CallerStructure<Derived_df> c;
    [[maybe_unused]] auto a = c.knownCall(42);
    [[maybe_unused]]  auto b = c.unknownCall(23., 11.f);
}

关于如何访问Base方法接口的可变参数模板的任何提示CallerStructure

强制编译器-资源管理器链接

标签: c++aliasvariadic-templatesusing

解决方案


您不能为可变参数模板参数设置别名。您可以将它们包装在std::tuple

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
    using UsableOtherArgs = std::tuple<OtherArgs...>;

    virtual OtherClass method(OtherArgs... a) = 0;
};

展开需要一些帮助:

template <typename DerivedOfBaseInstantiation,
          typename = typename DerivedOfBaseInstantiation::UsableOtherArgs>
struct CallerStructure;

template <typename DerivedOfBaseInstantiation, typename ... OtherArgs>
struct CallerStructure<DerivedOfBaseInstantiation, std::tuple<OthersArgs...>> {
    OtherClass knownCall(typename DerivedOfBaseInstantiation::UsableFirstArg a) {
        DerivedOfBaseInstantiation instance;
        return instance.someMethod(a);
    }
    OtherClass unknownCall(OtherArgs... args) {
        DerivedOfBaseInstantiation instance;
        return instance.method(args...);
    }
};

推荐阅读