Add boost::container_hash::is_tuple_like. Refs #30.

This commit is contained in:
Peter Dimov
2022-11-27 21:20:19 +02:00
parent fe28cdbd1f
commit c61a4f691e
3 changed files with 129 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
#ifndef BOOST_HASH_IS_TUPLE_LIKE_HPP_INCLUDED
#define BOOST_HASH_IS_TUPLE_LIKE_HPP_INCLUDED
// Copyright 2017, 2022 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/type_traits/integral_constant.hpp>
#include <boost/config.hpp>
#include <utility>
namespace boost
{
namespace hash_detail
{
template<class T, class E = true_type> struct is_tuple_like_: false_type
{
};
#if !defined(BOOST_NO_CXX11_HDR_TUPLE)
template<class T> struct is_tuple_like_<T, integral_constant<bool, std::tuple_size<T>::value == std::tuple_size<T>::value> >: true_type
{
};
#endif
} // namespace hash_detail
namespace container_hash
{
template<class T> struct is_tuple_like: hash_detail::is_tuple_like_<T>
{
};
} // namespace container_hash
} // namespace boost
#endif // #ifndef BOOST_HASH_IS_TUPLE_LIKE_HPP_INCLUDED

View File

@@ -117,3 +117,5 @@ run hash_is_avalanching_test.cpp ;
run hash_is_avalanching_test2.cpp ;
run hash_nullptr_test.cpp ;
run is_tuple_like_test.cpp ;

View File

@@ -0,0 +1,86 @@
// Copyright 2017, 2022 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/container_hash/is_tuple_like.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/config.hpp>
#include <utility>
struct X
{
};
#if !defined(BOOST_NO_CXX11_HDR_TUPLE)
#include <tuple>
namespace user
{
struct Y
{
int a;
int b;
};
template<std::size_t I> int& get( Y& v );
template<std::size_t I> int const& get( Y const& v );
template<> int& get<0>( Y& v )
{
return v.a;
}
template<> int const& get<0>( Y const& v )
{
return v.a;
}
template<> int& get<1>( Y& v )
{
return v.b;
}
template<> int const& get<1>( Y const& v )
{
return v.b;
}
} // namespace user
namespace std
{
template<> struct tuple_size<user::Y>: std::integral_constant<std::size_t, 2>
{
};
} // namespace std
#endif // #if !defined(BOOST_NO_CXX11_HDR_TUPLE)
int main()
{
using boost::container_hash::is_tuple_like;
BOOST_TEST_TRAIT_FALSE((is_tuple_like<void>));
BOOST_TEST_TRAIT_FALSE((is_tuple_like<int>));
BOOST_TEST_TRAIT_FALSE((is_tuple_like<X>));
BOOST_TEST_TRAIT_FALSE((is_tuple_like<int[2]>));
#if !defined(BOOST_NO_CXX11_HDR_TUPLE)
BOOST_TEST_TRAIT_TRUE((is_tuple_like< std::pair<int, X> >));
BOOST_TEST_TRAIT_TRUE((is_tuple_like< std::tuple<> >));
BOOST_TEST_TRAIT_TRUE((is_tuple_like< std::tuple<X> >));
BOOST_TEST_TRAIT_TRUE((is_tuple_like< std::tuple<X, X> >));
BOOST_TEST_TRAIT_TRUE((is_tuple_like< std::tuple<X, X, X> >));
BOOST_TEST_TRAIT_TRUE((is_tuple_like<user::Y>));
#endif
return boost::report_errors();
}