简体   繁体   English

C++17 可以可变参数模板 class 定义新的类型和可变参数数据成员吗?

[英]C++17 can variadic template class define new type and variadic data member?

#include <iostream>

template <typename T0, typename T1>
struct Clazz {
    void doSomething(const std::shared_ptr<T0>& p0,
            const std::shared_ptr<T1>& p1) {}
    T0 t0;
    T1 t1;
};
template <typename T0, typename T1, typename T2>
struct Clazz3 {
    void doSomething(const std::shared_ptr<T0>& p0,
            const std::shared_ptr<T1>& p1,
            const std::shared_ptr<T1>& p2) {}
    T0 t0;
    T1 t1;
    T2 t1;
};

template <typename... Ts>
struct ClazzN {
    // Q1: one part: define types in doSomething
    // Q2: two part: define data members
    // using param_type = typename (std::shared_ptr<Ts>, ...) // fold expression? can it works on type part, or only value?

    // Ts... ts; // ??? pack it by tuple ???
};

int main() {
    Clazz<int, double> c2;
    Clazz3<int, double, char> c3;

    return 0;
}

I have a class ClazN , and I:我有一个 class ClazN ,我:

  1. want a variadic template form, instead of limiting the class template type size.想要一个可变参数模板形式,而不是限制 class 模板类型大小。
  2. want define different data members as the template type correspodingly.希望将不同的数据成员定义为相应的模板类型。
  3. the T1 , T2 ... Tn have no relationship. T1 , T2 ... Tn没有关系。

How to write ClazzN ?如何编写ClazzN It's too stupid to write a lot repeated codes.写很多重复的代码太愚蠢了。

How could I do this?我怎么能这样做? Need your help, thanks a lot!!!需要您的帮助,非常感谢!!!

You can use std::tuple , also for the param_type .您可以使用std::tuple ,也可以用于param_type In case you want a tuple of shared pointers, that is:如果你想要一个共享指针的元组,那就是:

#include <tuple>
#include <iostream>
#include <memory>

template <typename... Ts>
struct Clazz {
    using param_type = typename std::tuple<std::shared_ptr<Ts>...>; 
    std::tuple<Ts...> ts;
};

int main() {
    Clazz<int, double> c0;
}

You could use std::tuple_element to access the list of types:您可以使用std::tuple_element访问类型列表:

#include <cstdint>
#include <tuple>
#include <string>

template <typename... Ts>
struct Clazz {
    template<std::size_t I>
    using T = std::tuple_element_t<I, std::tuple<Ts...>>;
};


using Test = Clazz<int, int&, double, std::string>;
static_assert(std::is_same_v<Test::T<0>, int>, "");
static_assert(std::is_same_v<Test::T<1>, int&>, "");
static_assert(std::is_same_v<Test::T<2>, double>, "");
static_assert(std::is_same_v<Test::T<3>, std::string>, "");

https://gcc.godbolt.org/z/dvadxrbEM https://gcc.godbolt.org/z/dvadxrbEM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++ 17 Variadic模板折叠 - C++17 Variadic Template Folding 使用新的 c++14 / c++17 功能改进可变参数模板 function - Improving a variadic template function using new c++14 / c++17 features 返回与C ++ 17中可变参数模板相对应的元组 - Return a tuple corresponding to variadic template in C++17 如何在C ++ 17中创建一个从变量模板推导出的类型向量元组? - How to create a tuple of vectors of type deduced from a variadic template in C++17? 检查模板类型T是否是C ++ 17中可变参数包的一部分 - Checking that a template type T is part of a variadic parameter pack in C++17 C++17 如何使用可变参数模板模仿 Julia 的“promote_type” function - C++17 How to mimic Julia's 'promote_type' function using variadic template C++17 - 可变参数模板 arguments - 使用模板参数调用 class 方法返回值 - C++17 - variadic template arguments - call class method on return value from previous with templated argument 在C ++ 17中定义可变坐标(元组)类型? - defining a variadic coordinate (tuple) type in C++17? 返回 C++17 可变参数模板“构造推导指南”的可变参数聚合(结构)和语法 - Returning variadic aggregates (struct) and syntax for C++17 variadic template 'construction deduction guide' 在c ++ 17中解压缩可变元组 - Unpacking variadic tuples in c++17
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM