example3_variadic_template_class.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <tuple>

template<typename... Args>
class SimpleTuple {
public:
    SimpleTuple(Args... args) : data(args...) {}

    void print() const {
        printTuple(data);
    }

private:
    std::tuple<Args...> data;

    template<std::size_t I = 0>
    void printTuple(const std::tuple<Args...>& t) const {
        if constexpr (I < sizeof...(Args)) {
            std::cout << std::get<I>(t) << " ";
            printTuple<I + 1>(t);
        } else {
            std::cout << std::endl;
        }
    }
};

int main() {
    SimpleTuple<int, double, std::string> tuple(1, 2.5, "Hello");
    tuple.print();  // Outputs: 1 2.5 Hello
    return 0;
}
Back to variadic_template