Don't worry if you cannot reproduce the resultsresults of the above two examples result of the above two examplessecond example. It's undefined behavior so anything (including but not limited to your cat getting pregnant) could happen.
if TInput is a builtin type like int, the variable user_input_value is not initialized on line 1. If the input on line 3 succeeds, the value is set to the input, which is fine. However, if the input is invalid, no value will be assigned and you'll return the uninitialized value. If the inputstream is good(), the extraction operator on line 3 succeeds,will either successfully extract and assign the value or, if invalid input is given, set to the input, which is finefailbit and assign 0. HoweverTherefore, if the first input is invalid, no value0 will be assignedreturned (for ounces) and you'll returnthe failbit set. Then, on the second entry, nothing is assigned to user_input_value and an uninitialized valueint returned (for age). This results in undefined behavior.
Running the above examplesabove examples second (“too many ounces of beer”) example through a tool like Valgrind can unveil the error. (To my surprise, neither ASan nor UbSan were able to detect the error.)
I originally thought that if input fails for whatever reason, the destination value would never be changed. This seemed to be the case but apparently was changed in C++11 such that now 0 is assigned for invalid input provided the stream was good() to begin with. Thanks to Mooing Duck for discovering this (see comments).
As Mooing Duck points out, compilers ought to ignore unknown #pragmas. However, it is still a good idea to make them conditional. For example, if you compile with -Werror=unknown-pragmas (And you should, because it is enabled by -Wall -Werror.), GCC will reject code with unknown #pragmas. This is conforming as in standard configuration, GCC ignores them gracefully as it is supposed to. Your library should not force your users to use less rigorous warning levels. Such “noisy” or “unclean” libraries are very annoying, to say the least.