From 7969de6fae8021830b9aab1513b073a0ab93211b Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Sun, 31 Jan 2016 19:04:37 +1100 Subject: [PATCH] Add C++11 initializer_list constructor and assignment to polynomial class. --- doc/internals/polynomial.qbk | 1 + include/boost/math/tools/polynomial.hpp | 17 +++++++++++++++++ test/test_polynomial.cpp | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/doc/internals/polynomial.qbk b/doc/internals/polynomial.qbk index 0961d8be6..5ea2e53b0 100644 --- a/doc/internals/polynomial.qbk +++ b/doc/internals/polynomial.qbk @@ -25,6 +25,7 @@ polynomial(I first, I last); template explicit polynomial(const U& point); + polynomial(std::initializer_list l); // C++11 // access: size_type size()const; diff --git a/include/boost/math/tools/polynomial.hpp b/include/boost/math/tools/polynomial.hpp index 9e187cca5..c9a1d4a37 100644 --- a/include/boost/math/tools/polynomial.hpp +++ b/include/boost/math/tools/polynomial.hpp @@ -25,6 +25,9 @@ #include #include #include +#if __cplusplus >= 201103L +#include +#endif namespace boost{ namespace math{ namespace tools{ @@ -276,6 +279,20 @@ public: m_data.push_back(boost::math::tools::real_cast(p[i])); } } + +#if __cplusplus >= 201103L + polynomial(std::initializer_list l) : polynomial(std::begin(l), std::end(l)) + { + } + + polynomial& + operator=(std::initializer_list l) + { + m_data.assign(std::begin(l), std::end(l)); + return *this; + } +#endif + // access: size_type size()const { return m_data.size(); } diff --git a/test/test_polynomial.cpp b/test/test_polynomial.cpp index f41fe579e..d5555a3d2 100644 --- a/test/test_polynomial.cpp +++ b/test/test_polynomial.cpp @@ -47,6 +47,26 @@ boost::array const d2 = {{-6, 0, 9}}; boost::array const d5 = {{-9, 0, 3, 0, -15}}; +BOOST_AUTO_TEST_CASE( test_construction ) +{ + polynomial const a(d3a.begin(), d3a.end()); + polynomial const b(d3a.begin(), 3); + BOOST_CHECK_EQUAL(a, b); +} + + +#if __cplusplus >= 201103L +BOOST_AUTO_TEST_CASE( test_initializer_list_construction ) +{ + polynomial a(begin(d3a), end(d3a)); + polynomial b = {10, -6, -4, 3}; + polynomial c{10, -6, -4, 3}; + BOOST_CHECK_EQUAL(a, b); + BOOST_CHECK_EQUAL(b, c); +} +#endif + + BOOST_AUTO_TEST_CASE( test_degree ) { polynomial const zero = zero_element(std::multiplies< polynomial >());