” Why does the T object still prints 100 after the catch block?
Because throwing is by value, creating a copy.
” What is the use of reference syntax in this case over pass by value?
Nothing.
Catching by reference to const
is a good rule of thumb, because it's generally efficient and safe.
The above, as the code was when I wrote this, doesn't catch by reference to const
, so it's just (1)ungood practice.
In passing, talking of good practice, using all uppercase names risks collision with macro names, and (in this case misguidingly) indicates macro to a trained reader.
Single letter uppercase names, and in particular T
, is to some degree a special case, because they have by convention been used for template parameters, so they're unlikely to be used as macro names.
Still, I recommend the old convention, all uppercase for macros, always, and mixed or lowercase for anything else.
1) At one time the wording of the C++98 and C++03 standards could be interpreted in a way where only catching by reference to non-const
guaranteed efficiency. It was a peculiar interpretation but advocated by at least one well-known C++ person. It's just of historic interest now.