Skip to main content
Bounty Awarded with 500 reputation awarded by rolfl
Included remarks by Williham Totland.
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29

Williham Totland commented that there is another free software library for line-editing, LinenoiseLinenoise. While checking its website, I also found the Editline Library (libedit) (also free software). I've never used either of them.

Williham Totland commented that there is another free software library for line-editing, Linenoise. While checking its website, I also found the Editline Library (libedit) (also free software). I've never used either of them.

Williham Totland commented that there is another free software library for line-editing, Linenoise. While checking its website, I also found the Editline Library (libedit) (also free software). I've never used either of them.

Included remarks by Williham Totland.
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29

Williham Totland commented that there is another free software library for line-editing, Linenoise. While checking its website, I also found the Editline Library (libedit) (also free software). I've never used either of them.

Williham Totland commented that there is another free software library for line-editing, Linenoise. While checking its website, I also found the Editline Library (libedit) (also free software). I've never used either of them.

Included remarks by Mooning Duck.
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29

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.

Don't worry if you cannot reproduce the results of the above two examples. 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. Running the above examples through a tool like Valgrind can unveil the error. (To my surprise, neither ASan nor UbSan were able to detect the error.)

Don't worry if you cannot reproduce the results of the above two examples result of the second 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 stream is good(), the extraction operator on line 3 will either successfully extract and assign the value or, if invalid input is given, set the failbit and assign 0. Therefore, if the first input is invalid, 0 will be returned (for ounces) and the failbit set. Then, on the second entry, nothing is assigned to user_input_value and an uninitialized int returned (for age). This results in undefined behavior. 

Running the above 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.

tweaked the "genericity" section a little
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
added 5 characters in body
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading
Source Link
5gon12eder
  • 4.3k
  • 14
  • 29
Loading