/* Copyright (C) 2016 Murray Cumming
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see .
*/
#ifndef SIGC_TUPLE_UTILS_TUPLE_END_H
#define SIGC_TUPLE_UTILS_TUPLE_END_H
#include
namespace sigc::internal
{
namespace detail
{
template
struct tuple_type_end_impl
{
using type = typename tuple_type_end_impl>::type,
remove_from_start - 1>::type;
};
template
struct tuple_type_end_impl
{
using type = T;
};
} // detail namespace
/**
* Get the type of a tuple with the last @a len types of the original.
*/
template
struct tuple_type_end : detail::tuple_type_end_impl::value - len>
{
};
/**
* Get the tuple with the last @a len items of the original.
*/
template
constexpr decltype(auto) // typename tuple_type_end::type
tuple_end(T&& t)
{
// We use std::decay_t<> because tuple_size is not defined for references.
constexpr auto size = std::tuple_size>::value;
static_assert(len <= size, "The tuple size must be greater than or equal to the length.");
if constexpr (len == 0)
{
// Prevent 'unreferenced formal parameter' warning from MSVC by 'using' t
static_cast(t);
// Recursive calls to tuple_cdr() would result in this eventually,
// but this avoids the extra work:
return std::tuple<>();
}
else if constexpr (size - len == 0)
{
return std::forward(t);
}
else if constexpr (size - len == 1)
{
return tuple_cdr(std::forward(t));
}
else
{
return tuple_end(tuple_cdr(std::forward(t)));
}
}
} // namespace sigc::internal;
#endif // SIGC_TUPLE_UTILS_TUPLE_END_H