Permalink
Browse files

Fix the mapping of containers when mapping to references

  • Loading branch information...
Naios committed Jul 11, 2017
1 parent 444b431 commit 7c23554788a6fefbc42a061c482ee91daf8270aa
Showing with 50 additions and 2 deletions.
  1. +13 −2 hpx/util/detail/pack_traversal_impl.hpp
  2. +37 −0 tests/unit/util/pack_traversal.cpp
@@ -221,11 +221,22 @@ namespace util {
decltype(std::move(*(std::declval<Container>().begin()))),
decltype(*(std::declval<Container>().begin()))>::type;
/// Removes all qualifier and references from the given type
/// if the type is a l-value or r-value reference.
template <typename T>
using dereferenced_of_t =
typename std::conditional<std::is_reference<T>::value,
typename std::decay<T>::type, T>::type;
/// Returns the type which is resulting if the mapping is applied to
/// an element in the container.
///
/// Since standard containers don't allow to be instantiated with
/// references we try to construct the container from a copied
/// version.
template <typename Container, typename Mapping>
using mapped_type_from_t =
typename invoke_result<Mapping, element_of_t<Container>>::type;
using mapped_type_from_t = dereferenced_of_t<
typename invoke_result<Mapping, element_of_t<Container>>::type>;
/// We create a new container, which may hold the resulting type
template <typename M, typename T>
@@ -628,6 +628,43 @@ static void testStrategicContainerTraverse()
nested.begin(), nested.end(), [](int i) { return i == 2; });
})));
}
/// - Ensure correct container remapping when returning references
{
// l-value references
{
std::vector<std::unique_ptr<int>> container;
container.push_back(std::unique_ptr<int>(new int(7)));
std::vector<int> res = map_pack(
[](std::unique_ptr<int> const& ref) -> int const& {
// ...
return *ref;
},
container);
HPX_TEST_EQ(res.size(), 1U);
HPX_TEST_EQ(res[0], 7);
}
// r-value references
{
std::vector<std::unique_ptr<std::unique_ptr<int>>> container;
container.push_back(std::unique_ptr<std::unique_ptr<int>>(
new std::unique_ptr<int>(new int(7))));
std::vector<std::unique_ptr<int>> res = map_pack(
[](std::unique_ptr<std::unique_ptr<int>> &
ref) -> std::unique_ptr<int>&& {
// ...
return std::move(*ref);
},
container);
HPX_TEST_EQ(res.size(), 1U);
HPX_TEST_EQ((*res[0]), 7);
}
}
}
static void testStrategicTupleLikeTraverse()

0 comments on commit 7c23554

Please sign in to comment.