c++ - 从基类重用模板变量
问题描述
让我们考虑这样的类层次结构
template<typename T>
struct Base
{
template<typename...U>
constexpr static bool SOME_TRAIT = std::is_constructible_v<T,U...>;
};
template<typename T>
struct Derived : Base<T>
{
//some usage of SOME_TRAIT
};
要在其中使用它,Dervied
我必须使用类似的东西:
template<typename...U>
constexpr static bool SOME_TRAIT = Base<T>::template SOME_TRAIT<U...>;
这可能很吵。是否有类似using
成员函数的声明,但可用于SOME_TRAIT
?
解决方案
没有。或者,更确切地说,您已经找到了最好的方法。
问题是,解析器需要对它进行SOME_TRAIT
解释。using SOME_TRAIT
不会向它提供更多关于<
and >
in是否SOME_TRAIT<foo>
应该被解释为运算符或分隔符的信息。语言标准要求在定义的时候完成Derived
,而不是等到Derived
实际专门化和使用,所以它不能假设它SOME_TRAIT
总是一个变量模板(因为Base
稍后可能会被赋予专门化)。bool
除了和命名之外,您在那里提供U
的是解析器完成其工作所需的最少信息量。
推荐阅读
- wso2 - 具有可扩展流量管理器 jndi 配置的 WSO2 API 管理器部署在文档中包含 QueueConnectionFactory 的“/test”
- ios - 如何在 Xamarin 中处理 iOS 生物识别身份验证中的错误情况?
- mysql - 如何将表 2 中的列包含到表 1 中?
- mysql - 将 IN 子查询的结果插入单独的列
- cron - mysqldump 备份文件显示无法打开输入文件:Mysqldump
- python - python构造函数中的参数不匹配
- google-oauth - 如何创建 Google API 作为代理?
- php - 在 WooCommerce 中恢复已删除的首页
- c++ - 使用信号检测子进程何时终止的最佳方法是什么
- c# - 没有奖励网格的 Q-Learning