From 1dd570a80056ac7400a69dc9cfeaa46d0d66357f Mon Sep 17 00:00:00 2001 From: formaggia Date: Fri, 23 Feb 2024 11:46:06 +0100 Subject: [PATCH] updated to eliminate deprecated stuff --- Examples/src/Bindings/main.cpp | 5 +++-- Examples/src/Functors/functors.cpp | 7 +++---- Examples/src/Functors/functors.hpp | 22 ++++++---------------- Examples/src/Functors/main_functors.cpp | 3 ++- Examples/src/Utilities/test_cppversion.cpp | 2 +- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Examples/src/Bindings/main.cpp b/Examples/src/Bindings/main.cpp index 7f506fe8e..adcf9afda 100644 --- a/Examples/src/Bindings/main.cpp +++ b/Examples/src/Bindings/main.cpp @@ -48,6 +48,7 @@ foo(int &a) * * @param a */ + void foo(const int &a) { @@ -146,9 +147,9 @@ main() std::cout << "foo does not implement overloads with rvalue-references\n"; std::cout << "calling foo(25) (a literal):" << std::endl; foo(25); // foo(const int&) - std::cout << "calling foo(a) (a is int, a lvalue):" << std::endl; + std::cout << "calling foo(a) (a is int, a non-const lvalue):" << std::endl; foo(a); // foo(int&) - std::cout << "calling foo(b) (b is int&, a lvalue):" << std::endl; + std::cout << "calling foo(b) (b is int&, a non-const lvalue):" << std::endl; foo(b); // foo(int&) std::cout << "calling foo(createFive()), I am passing a rvalue:" << std::endl; diff --git a/Examples/src/Functors/functors.cpp b/Examples/src/Functors/functors.cpp index 2859dc18a..54ce92ebf 100644 --- a/Examples/src/Functors/functors.cpp +++ b/Examples/src/Functors/functors.cpp @@ -10,15 +10,14 @@ Sqrt5::operator()(double a) const // df =5*y^4 if(a == 0.0) return 0.0; - auto der = [](double const x) { return 5 * x * x * x * x; }; - auto resid = [&a](double const x) { return x * x * x * x * x - a; }; + auto phi =[&a](double const x) { return (a/x/x/x/x -x ) / 5; }; auto err = 2 * tolerance; unsigned int iter = 0; auto x = x0; while((err > tolerance) && (iter < maxiter)) { - // I should check if der=0 but here I avoid for simplicity - auto step = -resid(x) / der(x); + // I should + auto step = phi(x); err = std::abs(step); x += step; ++iter; diff --git a/Examples/src/Functors/functors.hpp b/Examples/src/Functors/functors.hpp index 3d6a301b7..1a0f767b3 100644 --- a/Examples/src/Functors/functors.hpp +++ b/Examples/src/Functors/functors.hpp @@ -18,16 +18,13 @@ namespace myfunctors { /*! * - * A functor (object function) that takes a single argument is also called a - * A unary functor. - * - * It is good practice to inherit from std::unary_function, - * so that it complies with the unary functors in the - * standard library (It is not compulsory though,! you can do without) - * This object function computes fifth root of a by Newton method. Definition in + * @brief Computes the fitfh root of a number by Newton method + * + * A functor (object function) that takes a single argument is also called aunary functor. + * This object function computes fifth root of a by Newton method up to 10^-6 precision. Definition in * the cpp file */ -struct Sqrt5 : public std::unary_function +struct Sqrt5 { double operator()(double a) const; //! max number of iterations (default=100) @@ -44,15 +41,8 @@ struct Sqrt5 : public std::unary_function that returns true if the value passed is greater than a value that may be changed run time. - Since all methods are simple I will define them in class (no need - of cpp file) It is a unary function so I inherit from the - corresponding std class (not strictly needed, but it makes it - complient with the stl rules). Inheriting publicly from - unary_function provides two typedef members argument_type and - result_type - */ -class Isgreater : public std::unary_function +class Isgreater { public: Isgreater(double const &a) : my_value{a} {}; diff --git a/Examples/src/Functors/main_functors.cpp b/Examples/src/Functors/main_functors.cpp index 25d7bdaf5..470b4fbfd 100644 --- a/Examples/src/Functors/main_functors.cpp +++ b/Examples/src/Functors/main_functors.cpp @@ -3,6 +3,7 @@ #include #include #include +#include int main() { @@ -10,7 +11,7 @@ main() //! Creating an object Sqrt5 sqrt5; // comput fifth root of five - std::cout << std::setprecision(15) << "sqrt5(5)=" << sqrt5(5) << std::endl; + std::cout << std::setprecision(15) << "Exact value of fifth root of 5=" << std::pow(5,1./5.) << std::endl; // we have set a larger precision to see more digits. The setting // can be brought back to the default by // std::cout<