When I use Boost.Tuple, I have to use some syntax like:
result.get<0>()
It looks very unfamiliar to me. Usually <> contains a typename, why does it use an int here?
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.
The reason they use the syntax
result.get<0>()is that each element of the tuple can have a different type, and this syntax is the simplest way in C++ to let the compiler do the right thing with types.If the function were just plain
get(0), all elements of the tuple would have to have the same type, because there’s no way to have one untemplatedgetfunction that returns several different types.Something like
result.get<int>(0)could theoretically work, but it’s more verbose, and introduces a potential source of error–what if the 0th element wasn’t anintat all? Worse still, you’d only be able to catch this error at runtime. The syntax used in Boost is plain and simple–the only way you can possibly screw it up is to specify an invalid index, and that can be caught at compile time.