Dear ROOTers,
I noticed that in the ROOT main page (code snippet about the TTreeReader facility) you suggest the use of the “auto&&” in the range-based loops.
I learned (and it took some effort) that this is the most generic approach that can cover also corner cases like vector.
When are you showing this to total-beginners students (physicists not computer scientists), how do you sell this without mentioning “universal reference” and “reference collapsing”? The only answer that I have found so far is “believe me for the moment and if really interested read a book”.
Moreover, googling around, I understood that there is a corner case that is not well managed by this universal reference type deduction: if the reference is used in a function overloaded with both T& and const T& signatures, the non-const version would be silently selected, while a “const auto&” would address the selection to the const version.
Have I understood right this subtlety?
The fact that the compiler makes a silent choice isn’t worse than having a compiler error when I use “auto&” for a vector ? For a student would be very hard to guess the problem, while the compilation error would trigger maybe some question to an expert.
Especially in the framework of that code snippet about the access to TTree data, I typically want just to read the TTree data, and maybe a “const auto&” is more explicative of what I am doing, besides being easier to explain to beginners.
I would like to hear your opinions taking into account the specific environment of “physics students learning this analysis tool” rather than just a generic c++ discussion.
Thanks,
Matteo