Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missed opportunity for EBO in nested empty basic_tuples #333

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions include/boost/hana/basic_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/config.hpp>
#include <boost/hana/detail/decay.hpp>
#include <boost/hana/detail/ebo.hpp>
#include <boost/hana/detail/type_at.hpp>
#include <boost/hana/fwd/at.hpp>
#include <boost/hana/fwd/bool.hpp>
#include <boost/hana/fwd/concept/sequence.hpp>
Expand Down Expand Up @@ -72,7 +73,7 @@ BOOST_HANA_NAMESPACE_BEGIN
//////////////////////////////////////////////////////////////////////////
//! @cond
template <typename ...Xn>
struct basic_tuple final
struct basic_tuple
: detail::basic_tuple_impl<std::make_index_sequence<sizeof...(Xn)>, Xn...>
{
using Base = detail::basic_tuple_impl<std::make_index_sequence<sizeof...(Xn)>, Xn...>;
Expand Down Expand Up @@ -176,28 +177,75 @@ BOOST_HANA_NAMESPACE_BEGIN
//////////////////////////////////////////////////////////////////////////
template <>
struct at_impl<basic_tuple_tag> {
template <typename Xs, typename N>
static constexpr decltype(auto) apply(Xs&& xs, N const&) {
template <typename ...T, typename N>
static constexpr decltype(auto) apply(hana::basic_tuple<T...>& xs, N const&) {
constexpr std::size_t index = N::value;
using Nth = typename detail::type_at<index, T...>::type;
return detail::ebo_get<detail::bti<index>>(
static_cast<detail::ebo<detail::bti<index>, Nth>&>(xs)
);
}

template <typename ...T, typename N>
static constexpr decltype(auto) apply(hana::basic_tuple<T...>&& xs, N const&) {
constexpr std::size_t index = N::value;
return detail::ebo_get<detail::bti<index>>(static_cast<Xs&&>(xs));
using Nth = typename detail::type_at<index, T...>::type;
return detail::ebo_get<detail::bti<index>>(
static_cast<detail::ebo<detail::bti<index>, Nth>&&>(xs)
);
}

template <typename ...T, typename N>
static constexpr decltype(auto) apply(hana::basic_tuple<T...> const& xs, N const&) {
constexpr std::size_t index = N::value;
using Nth = typename detail::type_at<index, T...>::type;
return detail::ebo_get<detail::bti<index>>(
static_cast<detail::ebo<detail::bti<index>, Nth> const&>(xs)
);
}
};

template <>
struct drop_front_impl<basic_tuple_tag> {
template <std::size_t N, typename Xs, std::size_t ...i>
static constexpr auto drop_front_helper(Xs&& xs, std::index_sequence<i...>) {
template <std::size_t N, typename ...Xs, std::size_t ...i>
static constexpr auto
drop_front_helper(hana::basic_tuple<Xs...>&& xs, std::index_sequence<i...>) {
return hana::make_basic_tuple(
detail::ebo_get<detail::bti<i+N>>(static_cast<Xs&&>(xs))...
detail::ebo_get<detail::bti<i+N>>(
static_cast<detail::ebo<
detail::bti<i+N>, typename detail::type_at<i+N, Xs...>::type
>&&>(xs)
)...
);
}
template <std::size_t N, typename ...Xs, std::size_t ...i>
static constexpr auto
drop_front_helper(hana::basic_tuple<Xs...>& xs, std::index_sequence<i...>) {
return hana::make_basic_tuple(
detail::ebo_get<detail::bti<i+N>>(
static_cast<detail::ebo<
detail::bti<i+N>, typename detail::type_at<i+N, Xs...>::type
>&>(xs)
)...
);
}
template <std::size_t N, typename ...Xs, std::size_t ...i>
static constexpr auto
drop_front_helper(hana::basic_tuple<Xs...> const& xs, std::index_sequence<i...>) {
return hana::make_basic_tuple(
detail::ebo_get<detail::bti<i+N>>(
static_cast<detail::ebo<
detail::bti<i+N>, typename detail::type_at<i+N, Xs...>::type
> const&>(xs)
)...
);
}

template <typename Xs, typename N>
static constexpr auto apply(Xs&& xs, N const&) {
constexpr std::size_t len = detail::decay<Xs>::type::size_;
return drop_front_helper<N::value>(static_cast<Xs&&>(xs), std::make_index_sequence<
N::value < len ? len - N::value : 0
>{});
using Indices = std::make_index_sequence<N::value < len ? len - N::value : 0>;
return drop_front_helper<N::value>(static_cast<Xs&&>(xs), Indices{});
}
};

Expand All @@ -212,17 +260,26 @@ BOOST_HANA_NAMESPACE_BEGIN
// compile-time optimizations (to reduce the # of function instantiations)
template <std::size_t n, typename ...Xs>
constexpr decltype(auto) at_c(basic_tuple<Xs...> const& xs) {
return detail::ebo_get<detail::bti<n>>(xs);
using Nth = typename detail::type_at<n, Xs...>::type;
return detail::ebo_get<detail::bti<n>>(
static_cast<detail::ebo<detail::bti<n>, Nth> const&>(xs)
);
}

template <std::size_t n, typename ...Xs>
constexpr decltype(auto) at_c(basic_tuple<Xs...>& xs) {
return detail::ebo_get<detail::bti<n>>(xs);
using Nth = typename detail::type_at<n, Xs...>::type;
return detail::ebo_get<detail::bti<n>>(
static_cast<detail::ebo<detail::bti<n>, Nth>&>(xs)
);
}

template <std::size_t n, typename ...Xs>
constexpr decltype(auto) at_c(basic_tuple<Xs...>&& xs) {
return detail::ebo_get<detail::bti<n>>(static_cast<basic_tuple<Xs...>&&>(xs));
using Nth = typename detail::type_at<n, Xs...>::type;
return detail::ebo_get<detail::bti<n>>(
static_cast<detail::ebo<detail::bti<n>, Nth>&&>(xs)
);
}

//////////////////////////////////////////////////////////////////////////
Expand Down
18 changes: 18 additions & 0 deletions test/basic_tuple/drop_front.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/basic_tuple.hpp>
#include <boost/hana/drop_front.hpp>
#include <boost/hana/integral_constant.hpp>
namespace hana = boost::hana;


template <int i>
struct x { };

int main() {
// Make sure drop_front works with nested tuples
hana::basic_tuple<x<0>, hana::basic_tuple<x<1>, x<2>>> tuple;
auto all = hana::drop_front(tuple, hana::size_c<0>); (void)all;
}
27 changes: 27 additions & 0 deletions test/basic_tuple/empty_storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/basic_tuple.hpp>

#include <type_traits>
namespace hana = boost::hana;


struct empty1 { };
struct empty2 { };
struct empty3 { };


// Make sure the storage of a basic_tuple is compressed.
static_assert(sizeof(hana::basic_tuple<empty1, int>) == sizeof(int), "");
static_assert(sizeof(hana::basic_tuple<int, empty1>) == sizeof(int), "");

// Also make sure that a basic_tuple with only empty members is empty. This is
// important to ensure, for example, that a tuple of tuples of empty objects
// will get the EBO. We also test the nested case to be sure.
static_assert(std::is_empty<hana::basic_tuple<empty1, empty2>>{}, "");
static_assert(std::is_empty<hana::basic_tuple<empty1, hana::basic_tuple<empty2, empty3>>>{}, "");


int main() { }
20 changes: 12 additions & 8 deletions test/pair/empty_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
namespace hana = boost::hana;


// Make sure the storage of a pair is compressed
struct empty { };
static_assert(sizeof(hana::pair<empty, int>) == sizeof(int), "");
static_assert(sizeof(hana::pair<int, empty>) == sizeof(int), "");

// Also make sure that a pair with only empty members is empty too. This is
// important to ensure, for example, that a tuple of pairs of empty objects
// will get the EBO.
struct empty1 { };
struct empty2 { };
struct empty3 { };


// Make sure the storage of a pair is compressed.
static_assert(sizeof(hana::pair<empty1, int>) == sizeof(int), "");
static_assert(sizeof(hana::pair<int, empty1>) == sizeof(int), "");

// Also make sure that a pair with only empty members is empty. This is
// important to ensure, for example, that a tuple of pairs of empty
// objects will get the EBO. We also test the nested case.
static_assert(std::is_empty<hana::pair<empty1, empty2>>{}, "");
static_assert(std::is_empty<hana::pair<empty1, hana::pair<empty2, empty3>>>{}, "");


int main() { }