使用variadic模板
Using with variadic template
我知道以下代码编译:
template<class Type>
class Foo
{
using type = Type;
};
现在,我正在尝试编译以下代码:
template<class Type, class... OtherTypes>
class Foo
{
using type = Type;
// using types = OtherTypes;
// using... types = OtherTypes;
// using types... = OtherTypes;
// using types = OtherTypes...;
// using types... = OtherTypes...;
};
我在注释中尝试了代码的所有选项,但没有任何编译。我该如何修复?
您不能将类型类型作为类中的类型。
最接近的是:
template<class...Ts> struct types_t { constexpr types_t(){}; };
template<class...Ts> constexpr types_t<Ts...> types{};
这些是代表类型包的值和类型。
template<class Type, class... OtherTypes>
class Foo
{
using type=Type;
using types=types_t<OtherTypes...>;
};
然后,我们可以编写消耗捆绑类型并在其他地方使用它们的辅助功能。
template<template<class...>class Z, class Types>
struct apply_types;
template<template<class...>class Z, class...Ts>
struct apply_types<Z, types_t<Ts...>> {
using type=Z<Ts...>;
};
template<template<class...>class Z, class Types>
using apply_types_t = typename apply_types<Z,Types>::type;
现在apply_types< some_template, some_types_t >
将捆绑包中的类型带到模板中。
让我们假设要将包作为模板参数使用。然后,您可以尝试以下方法。
#include <utility>
template <class... Types>
struct Foo {};
template <template <class...> class Template,
class... Types,
template <class...> class T>
Template<Types...> foo(const T<Types...> &);
template <template <class...> class Template, class T>
using Type = decltype(foo<Template>(std::declval<T>()));
int main() {
using T = Foo<int, int>;
// As template argument
using Tuple = Type<std::tuple, T>;
static_assert(std::is_same<Tuple, std::tuple<int, int> >::value, "");
return 0;
}
相关文章:
- Variadic模板未编译
- variadic模板中的模板参数推导失败
- std::绑定variadic模板和自动返回类型
- is_same和variadic模板编译时错误无效转换
- 限制variadic模板类中的构造函数访问
- variadic函数模板:基于n编译时值在运行时间自动n输入
- variadic模板代码中的GCC VS MSVC编译误差
- 使用variadic模板和运行时索引构造iterator_range
- 将variadic模板功能转换为许多具体功能
- variadic模板折叠程序在GCC9中失败
- 如何在variadic模板参数中找到所有类的对象
- 如何访问variadic模板函数中的参数
- 来自整数的variadic模板参数
- 功能采用variadic模板包将std :: strings转换为const char *
- 模板模板参数,具有混合类型和非类型variadic参数
- 如何解释variadic模板中的继承
- 示例自定义共享_ptr-我如何传递构造函数参数 - 我在此处使用variadic模板
- C 11和类构造函数中的variadic模板
- variadic模板歧义 - 空参数包
- C++模板Variadic-为每个模板参数调用一个成员函数