Skip to content

Commit

Permalink
Add an is_tuple_like trait for sequenceable type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Naios committed Jun 7, 2017
1 parent f8e4b2f commit f7c555b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hpx/traits/is_tuple_like.hpp
@@ -0,0 +1,27 @@
// Copyright (c) 2017 Denis Blank
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#if !defined(HPX_TRAITS_IS_TUPLE_LIKE_HPP)
#define HPX_TRAITS_IS_TUPLE_LIKE_HPP

#include <type_traits>

#include <hpx/util/always_void.hpp>
#include <hpx/util/tuple.hpp>

namespace hpx { namespace traits
{
/// Deduces to a true type if the given parameter T
/// has a specific tuple like size.
template <typename T, typename = void>
struct is_tuple_like
: std::false_type { };
template <typename T>
struct is_tuple_like<T, typename util::always_void<decltype(
util::tuple_size<T>::value
)>::type> : std::true_type { };
}}

#endif
1 change: 1 addition & 0 deletions tests/unit/traits/CMakeLists.txt
Expand Up @@ -5,6 +5,7 @@

set(tests
is_callable
is_tuple_like
)

foreach(test ${tests})
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/traits/is_tuple_like.cpp
@@ -0,0 +1,47 @@
// Copyright (c) 2017 Denis Blank
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <utility>
#include <vector>

#if defined(HPX_HAVE_CXX11_STD_ARRAY)
#include <array>
#endif

#include <hpx/config.hpp>
#include <hpx/util/tuple.hpp>
#include <hpx/traits/is_tuple_like.hpp>
#include <hpx/util/lightweight_test.hpp>

void tuple_like_true()
{
using hpx::traits::is_tuple_like;

HPX_TEST_EQ((is_tuple_like<hpx::util::tuple<int, int, int>>::value), true);
HPX_TEST_EQ((is_tuple_like<std::pair<int, int>>::value), true);

#if defined(HPX_HAVE_CXX11_STD_ARRAY)
HPX_TEST_EQ((is_tuple_like<std::array<int, 4>>::value), true);
#endif
}

void tuple_like_false()
{
using hpx::traits::is_tuple_like;

HPX_TEST_EQ((is_tuple_like<int>::value), false);
HPX_TEST_EQ((is_tuple_like<std::vector<int>>::value), false);
}

///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
{
tuple_like_true();
tuple_like_false();
}

return hpx::util::report_errors();
}

0 comments on commit f7c555b

Please sign in to comment.