So say I define a tuple as such:
template<typename... Args>
class Tuple
{
Method () {...};
};
How do I define and access the instance variables for Tuple considering it can have an undefined number of them?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a couple of ways. The easiest one is to use structural recursion the way LISP does: a tuple is either
(head, tail)whereheadis the first element of the tuple andtailis a tuple containing the rest of the elements.In C++, this would look like the following:
Then you need a
get<n>function template to actually access the elements by index; it should be rather easy to implement if you grok how the tuple itself is recursively defined.As I said, there are other, trickier, implementation methods – for various reasons the above is not how most real-world
std::tupleimplementations do it.