From 736ab7a5556a5e6ddf93adea91baf4516abe830a Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 19 Apr 2024 16:47:38 +0200 Subject: [PATCH] make_array now use perfect forwarding, and remove fwd_make_array --- STL_Extension/include/CGAL/array.h | 40 ++++++++++--------- .../STL_Extension/test_fwd_make_array.cpp | 5 ++- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/STL_Extension/include/CGAL/array.h b/STL_Extension/include/CGAL/array.h index 7b15f064db87..98892c6257df 100644 --- a/STL_Extension/include/CGAL/array.h +++ b/STL_Extension/include/CGAL/array.h @@ -45,34 +45,36 @@ namespace CGAL { // It's also untrue that this is not documented... It is ! -template< typename T, typename... Args > -BOOST_CXX14_CONSTEXPR -std::array< T, 1 + sizeof...(Args) > -make_array(const T & t, const Args & ... args) -{ - std::array< T, 1 + sizeof...(Args) > a = { { t, static_cast(args)... } }; - return a; -} +template +struct Make_array_element_type { + using type = T; +}; -template< typename T, typename... Args > -BOOST_CXX14_CONSTEXPR -std::array< T, sizeof...(Args) > -fwd_make_array(Args && ... args) +template +struct Make_array_element_type { + using type = typename std::common_type_t; +}; + +template +using Make_array_element_type_t = typename Make_array_element_type::type; + +template +constexpr +std::array, sizeof...(Args) > +make_array(Args&& ... args) { - std::array< T, sizeof...(Args) > a = { static_cast(std::forward(args))... }; - return a; + return {{ std::forward(args)... }}; } - // Functor version struct Construct_array { - template + template constexpr - std::array - operator()(const T& t, const Args& ... args) const + decltype(auto) + operator()(Args&& ... args) const { - return make_array (t, args...); + return make_array( std::forward(args)... ); } }; diff --git a/STL_Extension/test/STL_Extension/test_fwd_make_array.cpp b/STL_Extension/test/STL_Extension/test_fwd_make_array.cpp index 305b09d3f1c1..6d55735a14f5 100644 --- a/STL_Extension/test/STL_Extension/test_fwd_make_array.cpp +++ b/STL_Extension/test/STL_Extension/test_fwd_make_array.cpp @@ -15,8 +15,9 @@ struct A // move-only class, move-constructible from B int main() { // this test requires C++17 mandatory return-value optimization (RVO) - std::array a = CGAL::fwd_make_array(B()); - std::array b = CGAL::fwd_make_array(1u); + std::array a = CGAL::make_array(B()); + auto b = CGAL::make_array(1u); + static_assert(std::is_same_v>); CGAL_USE(a); CGAL_USE(b); return 0;