【发布时间】:2021-02-11 13:40:03
【问题描述】:
考虑以下代码:
template<typename... Args1>
constexpr size_t direct = sizeof... (Args1);
template<template<typename... Args1> typename A1>
constexpr size_t nested = sizeof... (Args1); // compile error ☹️
嵌套行无法编译,因为(我认为)Args1 是嵌套参数。
有没有办法以非侵入性的方式解决这个问题?侵入性是指修改传递的类型,例如添加
constexpr size_t sizeof_args = sizeof... (Args...);
传递给这个模板的每个模板。
注意:我很擅长使用 C++20 概念,但 AFAIK 他们并没有在这个领域提供改进。
【问题讨论】:
-
您要使用
nested<std::vector>还是nested<std::vector<int>>? -
@Jarod42 不确定问题是什么,我最初想要的(在我将代码简化为问题中的代码之前)是比较第二个模板参数,例如 std::array
和 std::array 应该返回相同, std::map std::unordered_map 应该返回相同 ... -
std::array很特别,因为它混合了类型和非类型模板参数。但模板也可以有“模板模板参数”,因为std::vector不是类型,但std::vector<int>是。我们可能会为这两种情况编写特征。std::vector有 2 种类型参数(T和(默认)Allocator),std::vector<int/*, std::allocator<int>*/>也有 2 种类型。std::tuple没有固定数量的参数,std::tuple<int>有一个,std::tuple<int, char>有两个。
标签: c++ c++20 template-templates