Skip to content

Commit

Permalink
make_array now use perfect forwarding, and remove fwd_make_array
Browse files Browse the repository at this point in the history
  • Loading branch information
lrineau committed Apr 19, 2024
1 parent 4206382 commit 736ab7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
40 changes: 21 additions & 19 deletions STL_Extension/include/CGAL/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(args)... } };
return a;
}
template <typename T, typename ...Args>
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 <typename ...Args>
struct Make_array_element_type<void, Args...> {
using type = typename std::common_type_t<Args...>;
};

template <typename T, typename ...Args>
using Make_array_element_type_t = typename Make_array_element_type<T, Args...>::type;

template<typename T = void, typename... Args>
constexpr
std::array<Make_array_element_type_t<T, Args...>, sizeof...(Args) >
make_array(Args&& ... args)
{
std::array< T, sizeof...(Args) > a = { static_cast<T>(std::forward<Args>(args))... };
return a;
return {{ std::forward<Args>(args)... }};
}


// Functor version
struct Construct_array
{
template <typename T, typename... Args>
template <typename... Args>
constexpr
std::array<T, 1 + sizeof...(Args)>
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>(args)... );
}
};

Expand Down
5 changes: 3 additions & 2 deletions STL_Extension/test/STL_Extension/test_fwd_make_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, 1> a = CGAL::fwd_make_array<A>(B());
std::array<double, 1> b = CGAL::fwd_make_array<double>(1u);
std::array<A, 1> a = CGAL::make_array<A>(B());
auto b = CGAL::make_array<double>(1u);
static_assert(std::is_same_v<decltype(b), std::array<double, 1>>);
CGAL_USE(a);
CGAL_USE(b);
return 0;
Expand Down

0 comments on commit 736ab7a

Please sign in to comment.