c++ - 如何指定接受任何类型或值的参数包?
问题描述
在为类型指定参数包时,我希望不仅能够接受typename
s,而且能够接受任意类型的值。例子:
template <thing... Things>
// ^^^^^ <- pseudo code
struct Example {};
Example<int,bool>();
Example<4,float,'x'>();
显然,thing
不是一个有效的 C++ 关键字,但这里表达的想法是否可能?
上下文是我有一个容器适配器,它想要将自己的值类型插入到容器中,但允许传递任何其他模板参数,并且一些容器不仅接收类型参数,还接收值(std::array
将是一个流行的例子)。
template <template <typename...> typename Container, typename... Args>
struct Adapter {
struct Node { /* ... */ };
Container<Node,Args...> container;
};
Adapter<std::vector>(); // fine
Adapter<std::vector,MyAllocator>(); // fine
Adapter<std::array,3>(); // nope because 3 is not a typename
解决方案
推荐阅读
- css - 如何防止图像及其填充和边距溢出侧边栏?
- r - 如何在字符串列表之间放置一个空格?
- sql-server - 为什么小数的精度范围宽度存在差异?
- types - Agda 级别错误信息的含义: ... : .Agda.Primitive.Level
- python - 当 python 代码从 Windows 服务启动时,记录器文件轮换不起作用
- javascript - 与 react-router-dom 反应的粘性页脚
- laravel - Laravel Vue 变量
- jquery - 如果不调整浏览器窗口大小,HTML 模板无法正确加载
- css - 如何在 Flutter 行/列中重现 CSS flex-grow 空间分布行为?
- python - 刮困难表