diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cs0104_lesson_01.cpp | 104 | ||||
-rw-r--r-- | src/lesson20_oop_type.cpp | 2 | ||||
-rw-r--r-- | src/lesson23_oop_virtl02.cpp | 2 | ||||
-rw-r--r-- | src/lesson23_oop_virtl42.cpp | 228 | ||||
-rw-r--r-- | src/lesson39_stl_iterator.cpp | 12 | ||||
-rw-r--r-- | src/lesson40_algo01.cpp | 14 | ||||
-rw-r--r-- | src/lesson40_algo02.cpp | 8 |
7 files changed, 185 insertions, 185 deletions
diff --git a/src/cs0104_lesson_01.cpp b/src/cs0104_lesson_01.cpp index 57a0065..76dd2d0 100644 --- a/src/cs0104_lesson_01.cpp +++ b/src/cs0104_lesson_01.cpp @@ -418,7 +418,7 @@ static bool add_overflow(const short a, const short b, short &res) { // overflow: a + b > R+ -> a > R+ - b, with b >= 0 // underflow: a + b < R- -> a < R- - b, with b < 0 - + if ( ( b >= 0 && a > std::numeric_limits<short>::max() - b ) || ( b < 0 && a < std::numeric_limits<short>::min() - b ) ) { @@ -502,7 +502,7 @@ static void test03_1_4_immutable() { struct Class03_2_1 { int var = 0; - + int erhoeheUm(const int a) { var += a; return var; @@ -552,7 +552,7 @@ class Class03_2_3a { Class03_2_3a() noexcept { std::cout << "Class03_2_3a: Default-Constructor" << std::endl; } - + /** * Custom Constructor 1 * @param a init value for field_member_01 @@ -564,7 +564,7 @@ class Class03_2_3a { { std::cout << "Class03_2_3a: Custom-Constructor-1" << std::endl; } - + /// Copy Constructor /// default: `constexpr Class03_2_3a(const Class03_2_3a &o) noexcept = default;` Class03_2_3a(const Class03_2_3a &o) noexcept @@ -573,14 +573,14 @@ class Class03_2_3a { { std::cout << "Class03_2_3a: Copy-Constructor" << std::endl; } - + /// Move Constructor Class03_2_3a(Class03_2_3a &&o) noexcept = default; /// Copy-Assignment operator Class03_2_3a& operator=(const Class03_2_3a &o) noexcept = default; /// Move-Assignment operator Class03_2_3a& operator=(Class03_2_3a &&o) noexcept = default; - + /** * Destructor * @@ -592,18 +592,18 @@ class Class03_2_3a { ~Class03_2_3a() noexcept { std::cout << "Class03_2_3a: Destructor" << std::endl; } - + void increment() noexcept { ++field_member_01; // 'this' is a pointer to this instance (Object), where method `incement` is applied to ++this->field_member_02; } - + void reset() noexcept { field_member_01 = 1; field_member_02 = 0; } - + /** Two way comparison operator */ bool operator==(const Class03_2_3a& o) const noexcept { return field_member_01 + field_member_02 == o.field_member_01 + o.field_member_02; @@ -621,7 +621,7 @@ class Class03_2_3a { return std::strong_ordering::greater; } } - + /** Compare function returns 0 if equal, -1 if *this < b and 1 if *this > b. */ int compareTo(const Class03_2_3a& o) const noexcept { const int s1 = field_member_01 + field_member_02; @@ -641,27 +641,27 @@ class Class03_2_3a { h = ( ( h << 5 ) - h ) + field_member_01; return h; } - - /// std::ostream (stream out) free operator overload + + /// std::ostream (stream out) free operator overload std::ostream& streamout(std::ostream& out) const { return out << "Class03_2_3a[ref 0x" << std::hex << this - << std::dec - << ", 01 " << field_member_01 - << ", 02 " << field_member_01 + << std::dec + << ", 01 " << field_member_01 + << ", 02 " << field_member_01 << "]"; } - + std::string toString() const { std::stringstream ss; streamout(ss); return ss.str(); - } + } }; int Class03_2_3a::global_var01 = 1; // storage for global static and explicit initialization (init) int Class03_2_3a::global_var02; // storage for global static and w/o initialization -/// std::ostream (stream out) free operator overload +/// std::ostream (stream out) free operator overload std::ostream& operator<<(std::ostream& out, const Class03_2_3a& v) { return v.streamout(out); } @@ -685,13 +685,13 @@ static void test03_2_3a_class() { // 1) Compiler allocates stack-memory (memO1) for one instance of type Class03_2_3a // 2) Initialize this stack-memory (memO1) using Class03_2_3a's default constructor Class03_2_3a i1; - + // 3) Use memory (memO1) reference of this instance in o1 Class03_2_3a &o1 = i1; - - // instance variable i1 and its reference o1 use same memory address + + // instance variable i1 and its reference o1 use same memory address assert(&i1 == &o1); - + assert(1 == i1.field_member_01); assert(0 == i1.field_member_02); assert(1 == o1.field_member_01); @@ -702,10 +702,10 @@ static void test03_2_3a_class() { // 1) Compiler allocates stack-memory (memO2) for one instance of type Class03_2_3a // 2) Initialize this stack-memory (memO2) using Class03_2_3a's default constructor Class03_2_3a i2; - + // 3) Use memory (memO2) reference of this instance in o2 Class03_2_3a &o2 = i2; - + // // NOTE: This created instance i2 within memO2 in stack-memory (reference stored in o2) // is different than i1 within memO1 above (reference stored in o1) @@ -717,7 +717,7 @@ static void test03_2_3a_class() { assert(i1 == i2); assert(0 == i1.compareTo(o2)); assert(&i1 != &i2); // fast ref-check - + assert(o1 == o2); assert(0 == o1.compareTo(o2)); assert(&o1 != &o2); // fast ref-check @@ -776,7 +776,7 @@ static void test03_2_3a_class() { o1->reset(); assert(*o1 == *o2); assert(0 == o1->compareTo(*o2)); - + /// Manually delete heap-memory (unsafe!) delete o1; delete o2; @@ -835,45 +835,45 @@ static void test03_2_3a_class() { } } -/// RAII owner of heap allocated Class03_2_3a instance similar to std::unique_ptr +/// RAII owner of heap allocated Class03_2_3a instance similar to std::unique_ptr class Class03_2_3a_Owner { public: Class03_2_3a *m_o; - + Class03_2_3a_Owner() noexcept - : m_o(nullptr) {} - + : m_o(nullptr) {} + Class03_2_3a_Owner(Class03_2_3a* o) noexcept : m_o(o) {} - + /// Deleted Copy Constructor Class03_2_3a_Owner(const Class03_2_3a_Owner &o) noexcept = delete; /// Deleted Copy-Assignment operator Class03_2_3a_Owner& operator=(const Class03_2_3a_Owner &o) noexcept = delete; - + /// Default Move Constructor Class03_2_3a_Owner(Class03_2_3a_Owner &&o) noexcept = default; /// Default Move-Assignment operator Class03_2_3a_Owner& operator=(Class03_2_3a_Owner &&o) noexcept = default; - + ~Class03_2_3a_Owner() noexcept { if( nullptr != m_o ) { delete m_o; m_o = nullptr; } } - + /// may return nullptr, no exception Class03_2_3a* ptr() noexcept { return m_o; } - + /// throws std::runtime_error if nullptr! Class03_2_3a& ref() { if( nullptr == m_o ) { throw std::runtime_error("nullptr"); - } - return *m_o; - } + } + return *m_o; + } }; /// Resource acquisition is initialization @@ -897,7 +897,7 @@ struct Class03_3 { static int lala; // Konstante .. 3.3.2 Globale (Klassen) Variablen (immutable) static const int lulu = 0; - + // 3.3.3 Klassen Instanz Variable (Field) int lili = 0; }; @@ -906,26 +906,26 @@ int Class03_3::lala = 0; /** * 3.3 Speicher-Klassen von Variablen */ -static void test03_3_storage_class() { +static void test03_3_storage_class() { Class03_3 o1; Class03_3 o2; - + // 3.3.3 Klassen Instanz Variable (Field) assert(o1.lili == o2.lili); // gleich werte beider instanzen // 3.3.2 Globale (Klassen) Variablen assert(o1.lala == o2.lala); // gleicher wert der gemeinsamen globalen instanz - assert(Class03_3::lala == Class03_3::lala); // ditto - assert(o1.lulu == o2.lulu); // ditto - + assert(Class03_3::lala == Class03_3::lala); // ditto + assert(o1.lulu == o2.lulu); // ditto + // lili wert der instanz o1 auf 10 gesetzt (und nicht von der instanz o1) o1.lili = 10; assert(o1.lili != o2.lili); // o1.lili == 10, o2.lili == 0 - + // lala wert der Klasse (aller instanzen) auf 10 gesetzt o1.lala = 10; assert(o1.lala == o2.lala); // gleich - assert(Class03_3::lala == Class03_3::lala); // ditto + assert(Class03_3::lala == Class03_3::lala); // ditto } static void test03_5_arrays() { @@ -954,7 +954,7 @@ static void test03_5_arrays() { const int grades[] = { 1, 2, 3, 4, 4, 3, 2, 1, 5, 3, 1, 2, 3, 3, 2, 1, 5 }; const size_t length = sizeof(grades) / sizeof(int); - assert(17 == length); + assert(17 == length); int sum = 0; for(size_t i=0; i<length; ++i /* for-tail-statement */) { // NOLINT (intended) sum += grades[i]; @@ -989,7 +989,7 @@ static void test03_5_arrays() { sum += grades[i++]; } const float median = (float)sum / (float)length; - std::cout << "Median: " << median << " of " << length << " students. Sum " << sum << std::endl; + std::cout << "Median: " << median << " of " << length << " students. Sum " << sum << std::endl; } } @@ -1158,7 +1158,7 @@ static void test04_1_branch() { } } - // branches: conditional operator '?' + // branches: conditional operator '?' { { const int a = 0; @@ -1170,7 +1170,7 @@ static void test04_1_branch() { } { int x = 2; - + int l; if( 0 < x ) { l = 1; @@ -1185,10 +1185,10 @@ static void test04_1_branch() { } int result1 = l - r; assert(1 == result1); - + int result2 = (0 < x ? 1 : 0) - (x < 0 ? 1 : 0); assert(result1 == result2); - + // x<-1 -> result=-1 // x=-1 -> result=-1 // diff --git a/src/lesson20_oop_type.cpp b/src/lesson20_oop_type.cpp index 37208ee..80ec026 100644 --- a/src/lesson20_oop_type.cpp +++ b/src/lesson20_oop_type.cpp @@ -326,7 +326,7 @@ int main(int, char*[]) { { sint_t t(ten); - assert( eleven == ++t ); // increments and returns *this; NOLINT(bugprone-assert-side-effect) intended + assert( eleven == ++t ); // increments and returns *this; NOLINT(bugprone-assert-side-effect) intended assert( eleven == t ); } { diff --git a/src/lesson23_oop_virtl02.cpp b/src/lesson23_oop_virtl02.cpp index cebe9c9..1b2ffc9 100644 --- a/src/lesson23_oop_virtl02.cpp +++ b/src/lesson23_oop_virtl02.cpp @@ -16,7 +16,7 @@ class base_base_dog { { cout<< "base_dog::ctor begin" <<endl; bark() ; //NOLINT(clang-analyzer-optin.cplusplus.VirtualCall): intentional - + cout<< "base_dog::ctor end" <<endl; } diff --git a/src/lesson23_oop_virtl42.cpp b/src/lesson23_oop_virtl42.cpp index 1df7c1b..5168c8d 100644 --- a/src/lesson23_oop_virtl42.cpp +++ b/src/lesson23_oop_virtl42.cpp @@ -8,30 +8,30 @@ #include <iostream> /** - * test01 demos inheritance of two OO trees (wrong utilization): + * test01 demos inheritance of two OO trees (wrong utilization): * - exception, runtime_error, system_error: Mimiks non-virtual inheritance of std::exception * - ExceptionBase, RuntimeException, RuntimeSystemException: User virtual derived OO tree additionally inheriting from exception - * + * * Problem: - * - runtime_error, system_error not using virtual base class - * leading to multiple exception and runtime_error instances and ctor/dtor calls + * - runtime_error, system_error not using virtual base class + * leading to multiple exception and runtime_error instances and ctor/dtor calls */ namespace test01 { class exception { public: exception() { - std::cout<< "exception::ctor" <<std::endl; + std::cout<< "exception::ctor" <<std::endl; } - + virtual ~exception() { - std::cout<< "exception::dtor" <<std::endl; + std::cout<< "exception::dtor" <<std::endl; } virtual const char* what() const noexcept { return "exception::what()"; } }; - + class runtime_error : public exception { protected: std::string msg0; @@ -41,19 +41,19 @@ namespace test01 { { std::cout<< "runtime_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~runtime_error() override { std::cout<< "runtime_error::dtor: '" << msg0 << "'" <<std::endl; } - + const char* what() const noexcept override { static std::string s = "runtime_error::what("+msg0+")"; return s.c_str(); } - + }; - + class system_error : public runtime_error { public: system_error(const std::string& m) @@ -61,7 +61,7 @@ namespace test01 { { std::cout<< "system_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~system_error() override { std::cout<< "system_error::dtor: '" << msg0 << "'" <<std::endl; @@ -71,69 +71,69 @@ namespace test01 { return s.c_str(); } }; - + class ExceptionBase : public virtual exception { protected: std::string msg1; public: ExceptionBase(const std::string& m) - : exception(), msg1(m) + : exception(), msg1(m) { - std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; } - + ~ExceptionBase() override { - std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; } - + const std::string& whole_message() const noexcept { return msg1; } - + const char* what() const noexcept override { static std::string s = "ExceptionBase::what("+msg1+")"; return s.c_str(); } - + }; - + class RuntimeException : public virtual ExceptionBase, public virtual runtime_error { public: RuntimeException(std::string const& m) noexcept : exception(), ExceptionBase(m), runtime_error(m) { - std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeException() noexcept override { - std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeException(const RuntimeException& o) = default; RuntimeException(RuntimeException&& o) = default; RuntimeException& operator=(const RuntimeException& o) = default; RuntimeException& operator=(RuntimeException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeException::what("+msg1+")"; return s.c_str(); } - }; - + }; + class RuntimeSystemException : public virtual RuntimeException, public virtual system_error { public: RuntimeSystemException(std::string const& m) noexcept - : exception(), ExceptionBase(m), runtime_error(ExceptionBase::whole_message()), + : exception(), ExceptionBase(m), runtime_error(ExceptionBase::whole_message()), RuntimeException(m), system_error(whole_message()) { - std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeSystemException() noexcept override { - std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeSystemException(const RuntimeSystemException& o) = default; RuntimeSystemException(RuntimeSystemException&& o) = default; RuntimeSystemException& operator=(const RuntimeSystemException& o) = default; RuntimeSystemException& operator=(RuntimeSystemException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeSystemException::what("+msg1+")"; return s.c_str(); @@ -142,30 +142,30 @@ namespace test01 { } /** - * test02 demos fixed inheritance of two OO trees (fix 1): + * test02 demos fixed inheritance of two OO trees (fix 1): * - exception, runtime_error, system_error: Uses fixed virtual inheritance * - ExceptionBase, RuntimeException, RuntimeSystemException: User virtual derived OO tree additionally inheriting from exception - * + * * Fixed: - * - runtime_error, system_error is using virtual base class here, - * hence _single_ exception and runtime_error instances and ctor/dtor calls + * - runtime_error, system_error is using virtual base class here, + * hence _single_ exception and runtime_error instances and ctor/dtor calls */ namespace test02 { class exception { public: exception() { - std::cout<< "exception::ctor" <<std::endl; + std::cout<< "exception::ctor" <<std::endl; } - + virtual ~exception() { - std::cout<< "exception::dtor" <<std::endl; + std::cout<< "exception::dtor" <<std::endl; } virtual const char* what() const noexcept { return "exception::what()"; } }; - + class runtime_error : public virtual exception { protected: std::string msg0; @@ -175,19 +175,19 @@ namespace test02 { { std::cout<< "runtime_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~runtime_error() override { std::cout<< "runtime_error::dtor: '" << msg0 << "'" <<std::endl; } - + const char* what() const noexcept override { static std::string s = "runtime_error::what("+msg0+")"; return s.c_str(); } - + }; - + class system_error : public virtual runtime_error { public: system_error(const std::string& m) @@ -195,7 +195,7 @@ namespace test02 { { std::cout<< "system_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~system_error() override { std::cout<< "system_error::dtor: '" << msg0 << "'" <<std::endl; @@ -205,84 +205,84 @@ namespace test02 { return s.c_str(); } }; - + class ExceptionBase : public virtual exception { protected: std::string msg1; public: ExceptionBase(const std::string& m) - : exception(), msg1(m) + : exception(), msg1(m) { - std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; } - + ~ExceptionBase() override { - std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; } - + const std::string& whole_message() const noexcept { return msg1; } - + const char* what() const noexcept override { static std::string s = "ExceptionBase::what("+msg1+")"; return s.c_str(); } - + }; - + class RuntimeException : public virtual ExceptionBase, public virtual runtime_error { public: RuntimeException(std::string const& m) noexcept : exception(), ExceptionBase(m), runtime_error(m) { - std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeException() noexcept override { - std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeException(const RuntimeException& o) = default; RuntimeException(RuntimeException&& o) = default; RuntimeException& operator=(const RuntimeException& o) = default; RuntimeException& operator=(RuntimeException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeException::what("+msg1+")"; return s.c_str(); } - }; - + }; + class RuntimeSystemException : public virtual RuntimeException, public virtual system_error { public: RuntimeSystemException(std::string const& m) noexcept - : exception(), ExceptionBase(m), runtime_error(ExceptionBase::whole_message()), + : exception(), ExceptionBase(m), runtime_error(ExceptionBase::whole_message()), RuntimeException(m), system_error(whole_message()) { - std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeSystemException() noexcept override { - std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeSystemException(const RuntimeSystemException& o) = default; RuntimeSystemException(RuntimeSystemException&& o) = default; RuntimeSystemException& operator=(const RuntimeSystemException& o) = default; RuntimeSystemException& operator=(RuntimeSystemException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeSystemException::what("+msg1+")"; return s.c_str(); } - }; + }; } /** - * test03 demos inheritance of two OO trees (wrong utilization): + * test03 demos inheritance of two OO trees (wrong utilization): * - exception, runtime_error, system_error: Mimiks non-virtual inheritance of std::exception - * - ExceptionBase, RuntimeException, RuntimeSystemException: User non-virtual derived OO tree additionally + * - ExceptionBase, RuntimeException, RuntimeSystemException: User non-virtual derived OO tree additionally * selectively inheriting from exception, avoiding duplicated base classes. - * + * * Fixed: - * - runtime_error, system_error not using non-virtual base class, + * - runtime_error, system_error not using non-virtual base class, * but carefully inheriting from these leads to _single_ exception and runtime_error instances and ctor/dtor calls */ namespace test03 { @@ -290,17 +290,17 @@ namespace test03 { public: exception() { - std::cout<< "exception::ctor" <<std::endl; + std::cout<< "exception::ctor" <<std::endl; } - + virtual ~exception() { - std::cout<< "exception::dtor" <<std::endl; + std::cout<< "exception::dtor" <<std::endl; } virtual const char* what() const noexcept { return "exception::what()"; } }; - + class runtime_error : public exception { protected: std::string msg0; @@ -310,19 +310,19 @@ namespace test03 { { std::cout<< "runtime_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~runtime_error() override { std::cout<< "runtime_error::dtor: '" << msg0 << "'" <<std::endl; } - + const char* what() const noexcept override { static std::string s = "runtime_error::what("+msg0+")"; return s.c_str(); } - + }; - + class system_error : public runtime_error { public: system_error(const std::string& m) @@ -330,7 +330,7 @@ namespace test03 { { std::cout<< "system_error::ctor: '" << msg0 << "'" <<std::endl; } - + ~system_error() override { std::cout<< "system_error::dtor: '" << msg0 << "'" <<std::endl; @@ -340,91 +340,91 @@ namespace test03 { return s.c_str(); } }; - + class ExceptionBase { protected: std::string msg1; public: ExceptionBase(const std::string& m) - : msg1(m) + : msg1(m) { - std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::ctor '" << msg1 << "'" <<std::endl; } - + virtual ~ExceptionBase() { - std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "ExceptionBase::dtor '" << msg1 << "'" <<std::endl; } - + const std::string& whole_message() const noexcept { return msg1; } - + virtual const char* what() const noexcept { static std::string s = "ExceptionBase::what("+msg1+")"; return s.c_str(); } - + }; - + class RuntimeException : public ExceptionBase { protected: RuntimeException(std::string const& m) noexcept : ExceptionBase(m) { - std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::ctor '" << msg1 << "'" <<std::endl; } - - public: + + public: ~RuntimeException() noexcept override { - std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeException(const RuntimeException& o) = default; RuntimeException(RuntimeException&& o) = default; RuntimeException& operator=(const RuntimeException& o) = default; RuntimeException& operator=(RuntimeException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeException::what("+msg1+")"; return s.c_str(); } - }; - + }; + class RuntimeExceptionStd : public RuntimeException, public runtime_error { public: RuntimeExceptionStd(std::string const& m) noexcept : RuntimeException(m), runtime_error(m) { - std::cout<< "RuntimeExceptionStd::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeExceptionStd::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeExceptionStd() noexcept override { - std::cout<< "RuntimeExceptionStd::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeExceptionStd::dtor '" << msg1 << "'" <<std::endl; } - + const char* what() const noexcept override { static std::string s = "RuntimeException::what("+msg1+")"; return s.c_str(); } }; - + class RuntimeSystemException : public RuntimeException, public system_error { public: RuntimeSystemException(std::string const& m) noexcept : RuntimeException(m), system_error(whole_message()) { - std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::ctor '" << msg1 << "'" <<std::endl; } - + ~RuntimeSystemException() noexcept override { - std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; + std::cout<< "RuntimeSystemException::dtor '" << msg1 << "'" <<std::endl; } - + RuntimeSystemException(const RuntimeSystemException& o) = default; RuntimeSystemException(RuntimeSystemException&& o) = default; RuntimeSystemException& operator=(const RuntimeSystemException& o) = default; RuntimeSystemException& operator=(RuntimeSystemException&& o) = default; - + const char* what() const noexcept override { static std::string s = "RuntimeSystemException::what("+msg1+")"; return s.c_str(); } - }; + }; } int main() @@ -440,7 +440,7 @@ int main() } std::cout<<std::endl<<std::endl; std::cout<< "Main: test01::RuntimeSystemException Done" <<std::endl; - + { std::cout<< "Main: test02::RuntimeSystemException Create..." <<std::endl; test02::RuntimeSystemException rse("test02"); @@ -452,7 +452,7 @@ int main() } std::cout<<std::endl<<std::endl; std::cout<< "Main: test02::RuntimeSystemException Done" <<std::endl; - + { std::cout<< "Main: test03::RuntimeSystemException Create..." <<std::endl; test03::RuntimeSystemException rse("test03"); diff --git a/src/lesson39_stl_iterator.cpp b/src/lesson39_stl_iterator.cpp index c8758b8..c4cf70e 100644 --- a/src/lesson39_stl_iterator.cpp +++ b/src/lesson39_stl_iterator.cpp @@ -25,21 +25,21 @@ int main(int, const char**) { int* plala = &array[0]; for(size_t i=0; i<9l; ++i) { array[i] = *(plala + i) + 1; - } + } } - // Because std::vector<>::begin() iterator performs arithmetic - // using a signed difference_type, we need to use such a signed type + // Because std::vector<>::begin() iterator performs arithmetic + // using a signed difference_type, we need to use such a signed type // here to avoid `bugprone-narrowing-conversions` (LINT) // // Now, isn't this odd as std::vector<>::size() uses unsigned size_type, // aka size_t and mentioned iterator hence lose half the value range possible? - { + { // index operator[] can use unsigned size_t, full range std::vector<int> array(10, 0); for(size_t i=0; i<array.size(); ++i) { // using unsigned int as index is legal, also full range - array[i] += 1; + array[i] += 1; } // now iterating via iterator, loss of full range typedef std::vector<int>::difference_type iterdiff_t; @@ -47,7 +47,7 @@ int main(int, const char**) { for(iterdiff_t i=0; i<sz; ++i) { // begin() iterator is signed hence adding signed difference_type, loss of full range // same is true w/ all other operations like insert where an iterator is being used - *(array.begin() + i) += 1; + *(array.begin() + i) += 1; } // or .. same issues typedef std::vector<int>::iterator iter_t; diff --git a/src/lesson40_algo01.cpp b/src/lesson40_algo01.cpp index 2137b30..82113e5 100644 --- a/src/lesson40_algo01.cpp +++ b/src/lesson40_algo01.cpp @@ -48,19 +48,19 @@ bool test_binsearch1(binary_search_func1_t binary_search, const std::vector<int> constexpr static const ssize_t no_index_0 = -1; ssize_t binary_search00(const std::vector<int>& array, int target_value) { - // Because std::vector<>::begin() iterator performs arithmetic - // using a signed difference_type, we need to use such a signed type + // Because std::vector<>::begin() iterator performs arithmetic + // using a signed difference_type, we need to use such a signed type // here to avoid `bugprone-narrowing-conversions` (LINT) // // Now, isn't this odd as std::vector<>::size() uses unsigned size_type, // aka size_t and mentioned iterator hence lose half the value range possible? typedef std::vector<int>::difference_type iterdiff_t; iterdiff_t l = 0; - iterdiff_t h = array.cend() - array.cbegin() - 1; + iterdiff_t h = array.cend() - array.cbegin() - 1; iterdiff_t c = 0; while( l <= h ) { // iterdiff_t i = ( l + h ) / 2; // l+h too big? - iterdiff_t i = l + ( h - l ) / 2; // better, also solved with std::midpoint(l, h) + iterdiff_t i = l + ( h - l ) / 2; // better, also solved with std::midpoint(l, h) std::cout << "c " << c << " [" << l << ".." << h << "]: p " << i << std::endl; if ( array[i] < target_value ) { l = i + 1; @@ -132,8 +132,8 @@ size_t binary_search11(const std::vector<int>& array, int target_value) { } void test_binsearch0(binary_search_func0_t binary_search, std::vector<int>& array_in, std::vector<int>& array_miss, int line) { - // Because std::vector<>::begin() iterator performs arithmetic - // using a signed difference_type, we need to use such a signed type + // Because std::vector<>::begin() iterator performs arithmetic + // using a signed difference_type, we need to use such a signed type // here to avoid `bugprone-narrowing-conversions` (LINT) // // Now, isn't this odd as std::vector<>::size() uses unsigned size_type, @@ -148,7 +148,7 @@ void test_binsearch0(binary_search_func0_t binary_search, std::vector<int>& arra std::cout << "OK : array_in[" << i << "] = " << array_in[i] << " found at " << idx << std::endl; } } - + const ssize_t amiss_sz = array_miss.cend() - array_miss.cbegin(); for(ssize_t i=0; i < amiss_sz; ++i) { const int target = array_miss[i]; diff --git a/src/lesson40_algo02.cpp b/src/lesson40_algo02.cpp index 322e37e..7916fa4 100644 --- a/src/lesson40_algo02.cpp +++ b/src/lesson40_algo02.cpp @@ -53,9 +53,9 @@ size_t ordered_insert(std::vector<int>& array, int value) { if( array.size() < 1 ) { array.push_back(value); return 0; - } - // Because std::vector<>::begin() iterator performs arithmetic - // using a signed difference_type, we need to use such a signed type + } + // Because std::vector<>::begin() iterator performs arithmetic + // using a signed difference_type, we need to use such a signed type // here to avoid `bugprone-narrowing-conversions` (LINT) // // Now, isn't this odd as std::vector<>::size() uses unsigned size_type, @@ -73,7 +73,7 @@ size_t ordered_insert(std::vector<int>& array, int value) { // size_t c = 0; while( h - l >= 2 ) { // iterdiff_t i = ( l + h ) / 2; // l+h too big? - iterdiff_t i = l + ( h - l ) / 2; // better, also solved with std::midpoint(l, h) + iterdiff_t i = l + ( h - l ) / 2; // better, also solved with std::midpoint(l, h) // std::cout << "c " << c << " (" << l << ".." << h << "): p " << i << std::endl; if ( array[i] < value ) { l = i; |