diff options
Diffstat (limited to '6p30.cpp')
-rw-r--r-- | 6p30.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/6p30.cpp b/6p30.cpp new file mode 100644 index 0000000..064b56f --- /dev/null +++ b/6p30.cpp @@ -0,0 +1,35 @@ +#include <iostream> + +/* + * + * 6.30 + * + * + */ + +/* +bool str_subrange(const std::string &str1, const std::string &str2) +{ +// same sizes: return normal equality test + if (str1.size() == str2.size()) + return str1 == str2; + // ok: == returns bool + // find the size of the smaller string; conditional operator, see ยง 4.7 (p. 151) + auto size = (str1.size() < str2.size()) + ? str1.size() : str2.size(); + + // look at each element up to the size of the smaller string + for (decltype(size) i = 0; i != size; ++i) { + if (str1[i] != str2[i]) + return; // error #1: no return value; compiler should detect this error + } + // error #2: control might flow off the end of the function without a return + // the compiler might not detect this error +} +*/ + +int main () { + + // Tested both with clang++ and g++ and both detected the error + return 0; +} |