c++ - 给模板参数包起别名
问题描述
我想为参数包使用 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
是未知的,并且由CallerStructure
whereDerivedOfBaseInstantiation
是派生自 的类型的实例化确定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
?
解决方案
您不能为可变参数模板参数设置别名。您可以将它们包装在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...);
}
};
推荐阅读
- sql - SQL-SSRS 报告
- django - Django Oracle OCA-24450
- python - for循环中的Python“else”和“if not”语句
- intellij-idea - IntelliJ IDEA 无法编译 groovy 测试
- doctrine-orm - 持久化数据后刷新时出现致命错误
- python - django crud 服务器总是返回 404
- reactjs - 使用 refs 在父 AJAX 之后设置子输入值
- java - 如何改善 SQL 连接
- angular - Angular 属性指令 - prod 构建错误,预期 1 个参数但得到 0
- elasticsearch - Windows 无法在本地启动弹性搜索服务