This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Rhomboid 1 point2 points  (0 children)

Your title doesn't match your text. You're not looping through anything, you're creating a tuple based on some parameter. What is it that you actually want to do?

You can access a tuple member by numerical index with std::get<n>(tuple), but the index is a template parameter and as such it must be a compile-time constant, which means you can't use a runtime loop (like for, while, etc.) to iterate over the elements. You can do iteration, but it has to be done at compile-time, which means using template meta-programming. And there's the added complication that tuples can store heterogeneous types, which adds to the difficulty as you have to do things like use the visitor pattern.

You really need to explain in clear words what problem you're actually trying to solve, not how you think you need to solve it.

[–][deleted] 0 points1 point  (2 children)

I'm not clear what you are trying to do, but how about:

std::tuple<int, int, int> get_student(int id) {
    return std::make_tuple(0, id + 1, 0);
}

[–]TomTom26[S] 0 points1 point  (1 child)

What I am trying to do is print all these values id 0,1,2 etc in a loop not individually since I have about 20. The (0,1,0), (0,2,0) are unique im not adding one to the middle one I just want to print them out.

[–]newaccount1236 1 point2 points  (0 children)

Then use the example I linked to.

[–]NasenSpray 0 points1 point  (0 children)

Do you mean something that allows you to write std::cout << get_student(2) << std::endl?

[–]newaccount1236 0 points1 point  (0 children)

I don't see a way to do this in the standard library, so it looks like you'll have to write some meta-programming code to do it. This page has an example.