diff options
author | Oskar <[email protected]> | 2024-09-12 12:28:48 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-09-12 12:28:48 +0200 |
commit | 13c97461fde19829569dbe10f53322fef5909b83 (patch) | |
tree | 7f7f529c849c601184edc95b9a449e870e2854dc | |
parent | 70878e5c5b4552def51082829032780c536dc59d (diff) |
more
-rw-r--r-- | 6p30.cpp | 35 | ||||
-rw-r--r-- | 6p31.cpp | 13 | ||||
-rw-r--r-- | 6p32.cpp | 18 | ||||
-rw-r--r-- | 6p33.cpp | 26 | ||||
-rw-r--r-- | 6p34.cpp | 11 | ||||
-rw-r--r-- | 6p35.cpp | 11 | ||||
-rw-r--r-- | 6p36.cpp | 16 | ||||
-rw-r--r-- | 6p37.cpp | 19 | ||||
-rw-r--r-- | 6p38.cpp | 18 | ||||
-rw-r--r-- | 6p39.cpp | 23 | ||||
-rw-r--r-- | 6p40.cpp | 16 | ||||
-rw-r--r-- | 6p41.cpp | 20 | ||||
-rw-r--r-- | 6p42.cpp | 24 |
13 files changed, 250 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; +} diff --git a/6p31.cpp b/6p31.cpp new file mode 100644 index 0000000..49bfced --- /dev/null +++ b/6p31.cpp @@ -0,0 +1,13 @@ + +/* + * + * 6.31 + * + * + */ + +int main () { + + // When the reference is not a local variable + return 0; +} diff --git a/6p32.cpp b/6p32.cpp new file mode 100644 index 0000000..76d431c --- /dev/null +++ b/6p32.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +/* + * + * 6.32 + * + * + */ + +int &get(int *arry, int index) { + return arry[index]; +} + +int main() { + int ia[10]; + for (int i = 0; i != 10; ++i) + get(ia, i) = i; +} diff --git a/6p33.cpp b/6p33.cpp new file mode 100644 index 0000000..d72ab83 --- /dev/null +++ b/6p33.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <vector> + +/* + * + * 6.33 + * + * + */ + +void recursive_vec_print(std::vector<int>::const_iterator cbeg, std::vector<int>::const_iterator cend) { + + if(cbeg == cend) { + return; + } + + std::cout << *cbeg++ << std::endl; + recursive_vec_print(cbeg, cend); +} + +int main () { + + std::vector<int> myvec = {1,2,32,44,2,24,1,62,24}; + recursive_vec_print(myvec.cbegin(), myvec.cend()); + return 0; +} diff --git a/6p34.cpp b/6p34.cpp new file mode 100644 index 0000000..4ee9d25 --- /dev/null +++ b/6p34.cpp @@ -0,0 +1,11 @@ + +/* + * + * 6.34 + * + * + */ + +int main () { + return 0; +} diff --git a/6p35.cpp b/6p35.cpp new file mode 100644 index 0000000..2d39416 --- /dev/null +++ b/6p35.cpp @@ -0,0 +1,11 @@ + +/* + * + * 6.35 + * + * + */ + +int main () { + return 0; +} diff --git a/6p36.cpp b/6p36.cpp new file mode 100644 index 0000000..7183a90 --- /dev/null +++ b/6p36.cpp @@ -0,0 +1,16 @@ +#include <iostream> + +/* + * + * 6.36 + * + * + */ + +std::string (&myfunction())[10]; + +int main () { + + + return 0; +} diff --git a/6p37.cpp b/6p37.cpp new file mode 100644 index 0000000..4a7320c --- /dev/null +++ b/6p37.cpp @@ -0,0 +1,19 @@ +#include <iostream> + +/* + * + * 6.37 + * + * + */ + +std::string (&myfunction())[10]; + +auto myfunction2() -> std::string(&)[10]; // This is obviously the easiest way to do it + +std::string myarray[10]; +decltype(myarray) &myfunction3(); + +int main () { + return 0; +} diff --git a/6p38.cpp b/6p38.cpp new file mode 100644 index 0000000..978ee9f --- /dev/null +++ b/6p38.cpp @@ -0,0 +1,18 @@ +#include <iostream> + +/* + * + * 6.38 + * + * + */ +int odd[] = {1,3,5,7,9}; +int even[] = {0,2,4,6,8}; +auto arrPtr(int i) -> int (&)[5] { + + return (i % 2) ? odd : even; +} + +int main () { + return 0; +} diff --git a/6p39.cpp b/6p39.cpp new file mode 100644 index 0000000..36a218c --- /dev/null +++ b/6p39.cpp @@ -0,0 +1,23 @@ + +/* + * + * 6.39 + * + * + */ + +int calc(int, int); +int calc(const int, const int); +/* +int get(); +double get(); +*/ +int *reset(int *); +double *reset(double *); +int main () { + + // (A) Legal + // (B) Not legal + // (C) Legal + return 0; +} diff --git a/6p40.cpp b/6p40.cpp new file mode 100644 index 0000000..6c2c1ae --- /dev/null +++ b/6p40.cpp @@ -0,0 +1,16 @@ + +/* + * + * 6.40 + * + * + */ + +int ff(int a, int b = 0, int c = 0); +//char *init(int ht = 24, int wd, char bckgrnd); +int main () { + + // (A) OK + // (B) Error + return 0; +} diff --git a/6p41.cpp b/6p41.cpp new file mode 100644 index 0000000..2b3b61c --- /dev/null +++ b/6p41.cpp @@ -0,0 +1,20 @@ + +/* + * + * 6.41 + * + * + */ + +char *init(int ht, int wd = 80, char bckgrnd = ' ') { + + if(ht || wd || bckgrnd) {} + return 0; +} +int main () { + + // (A) illegal + // (B) legal + // (C) legal but not what the author intends, they think that the '*' will be the argument for bckgrnd when in reality the '*' character will be converted to an int + return 0; +} diff --git a/6p42.cpp b/6p42.cpp new file mode 100644 index 0000000..a2e1cca --- /dev/null +++ b/6p42.cpp @@ -0,0 +1,24 @@ +#include <iostream> + +/* + * + * 6.42 + * + * + */ + +std::string make_plural(size_t ctr, + const std::string &word, + const std::string &ending = "s" + ) { + + return (ctr > 1) ? word + ending : word; +} + +int main () { + + std::cout << make_plural(2, "Success", "es") << std::endl; + std::cout << make_plural(2, "Success") << std::endl; + std::cout << make_plural(2, "Failure") << std::endl; + return 0; +} |