Why is it better? AFAICT, you're doing that because you want the very minor syntax difference, but it requires the use of templates. The vector constructor I used is an iterator constructor. It's typical code to use arithmetic in order to generate iterators.
No, it saves you from manually inputting the size.
Originally Posted by whiteflags
Less manual size inputting = fewer bugs.
You know the size parameter doesn't have to be "manual" in the sense that there is a mutable variable or literal constant lying around.
I am perfectly aware that there are "safe" ways to do things, but better safe than sorry.
The time comes when you will do modifications to your code, and your size parameter suddenly becomes a hidden runtime bug. Not fun.
Yeah, I do agree. The time to change code is not the time to take a mental vacation, either so that would be my advice to newfriends.
Just wanted to throw this out there. I'd even prefer something like this in the single context this trick actually works.
Thing has a constructor with the dreaded size parameter of course.
template <typename T, std::size_t retval>
inline std::size_t SizeOf ( T (&foo)[retval] )
Thing thingconstructed(something, SizeOf(something));
Even more problematic: Ignoring the bad function name, in C++11 constexpr might work out better than inline anyway, but constexpr has rules, and this isn't important, so I can't be bothered. Applying the trick to every possible instance just grinds my gears.