PhilLab
PhilLab

Reputation: 5007

Getter for variadic template class

Is it possible to have a typed getter for a variadic template class? I.e. something like the following but which does compile:

template<class... T>
class A {
public:
    A(T... params) {
        _params = { params... };
    }
    T... get(const size_t index) {
        return _params.at(index);
    }
private:
    std::vector<T...> _params;
};

Which is then usable like:

A<std::string, int> foo(std::string("a"), 2);
std::string bar = foo.get(0).append("b");
int bar2 = foo.get(1) + 2;

Upvotes: 0

Views: 171

Answers (1)

TartanLlama
TartanLlama

Reputation: 65620

This is exactly what std::tuple is for.

auto foo = std::make_tuple(std::string("a"), 2);
std::string bar = std::get<0>(foo).append("b");
int bar2 = std::get<1>(foo) + 2;

Upvotes: 1

Related Questions