All new for C ++ 17
#include <optional> using optional_int = std::optional<int>; class foo { int arg0, arg1; // required int arg2, arg3; // optional const int default_2 = -2; const int default_3 = -3; public: foo(int arg0, int arg1, optional_int opt0 = {}, optional_int opt1 = {}) : arg0(arg0), arg1(arg1) , arg2(opt0.value_or(default_2)) , arg3(opt1.value_or(default_3)) { } }; int main() { foo bar(42, 43, {}, 45); // Take default for opt0 (arg2) return 0; }
I have a cubic spline implementation that allows the user to optionally indicate the first derivative either at the left end, and at the right end, or both. If the derivative is not specified, then the current code calculates one, assuming that the second derivative is zero (the so-called "natural spline"). Here is a snippet for the left end.
// Calculate the second derivative at the left end point if (!left_deriv.has_value()) { ddy[0]=u[0]=0.0; // "Natural spline" } else { const real yP0 = left_deriv.value(); ddy[0] = -0.5; u[0]=(3.0/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yP0); }
Jive Dadson Jan 20 '18 at 6:01 2018-01-20 06:01
source share