c++ - Inheriting a default template value
问题描述
I have multiple aliases for a class and would like those aliases to inherit the base class' default template argument. Here is a simple example of syntactically what I'm trying to achieve:
template<int f = 5>
class A {};
template<int T/*= 5*/>
using Test = A<T>;
int main()
{
A<> foo;
Test<> foo2; // error: wrong number of template arguments (0, should be 1)
}
Or do I have to resort to making the default value an explicitly accessible value?
static const int DefaultVal = 5;
template<int f = DefaultVal>
class A {};
template<int T= DefaultVal>
using Test = A<T>;
解决方案
您不能“继承”默认值。
另一种可能性是使用可变参数模板:
template <int ... Ts>
using Test = A<Ts...>;
这允许Test<>
这样。A<>
A<5>
但它“谎言”与 invalid Test<1, 2, 3, 4>
。
推荐阅读
- mysql - 如何删除 MySQL 的 JSON 列中的键
- c# - 使用 Angular 的服务器上的 WebApi 身份验证失败
- asp.net-mvc - 如何解决 Web api 错误“System.OutOfMemoryException”
- c# - 用于客户端和服务器端验证的 ASP.NET Core 重用代码
- java - 我似乎无法将 Java 库添加到 Apache Netbeans 11.3
- database - Er图/数据库管理
- linux - 如何检查之前的 Zsh 提示符中是否没有输入任何内容
- r - 用于二进制分类的 R 中的 Xgboost 超参数调整
- java - Odoo 移动框架 - BaseModel
- c# - 如何通过以下代码将 2D 精灵保持在屏幕内?