Initial implementation of template deduction guides

This commit is contained in:
Daniel James
2017-12-19 12:11:36 +00:00
parent f99dee1917
commit b50e0d610f
5 changed files with 597 additions and 0 deletions

View File

@ -193,6 +193,18 @@
#endif
#endif
// BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
#if !defined(BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES)
#if BOOST_COMP_CLANG && __cplusplus >= 201703
#define BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES 1
#endif
#endif
#if !defined(BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES)
#define BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES 0
#endif
namespace boost {
namespace unordered {
namespace iterator_detail {

View File

@ -831,6 +831,81 @@ namespace boost {
#endif
}; // class template unordered_map
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
namespace detail {
template <typename T>
using iter_key_t =
typename std::iterator_traits<T>::value_type::first_type;
template <typename T>
using iter_val_t =
typename std::iterator_traits<T>::value_type::second_type;
template <typename T>
using iter_to_alloc_t =
typename std::pair<iter_key_t<T> const, iter_val_t<T> >;
}
template <class InputIterator,
class Hash =
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
class Pred =
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
class Allocator = std::allocator<
boost::unordered::detail::iter_to_alloc_t<InputIterator> > >
unordered_map(InputIterator, InputIterator,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_map<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>, Hash, Pred,
Allocator>;
template <class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T> > >
unordered_map(std::initializer_list<std::pair<const Key, T> >,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_map<Key, T, Hash, Pred, Allocator>;
template <class InputIterator, class Allocator>
unordered_map(InputIterator, InputIterator, std::size_t, Allocator)
->unordered_map<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>,
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class InputIterator, class Allocator>
unordered_map(InputIterator, InputIterator, Allocator)
->unordered_map<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>,
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class InputIterator, class Hash, class Allocator>
unordered_map(InputIterator, InputIterator, std::size_t, Hash, Allocator)
->unordered_map<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>, Hash,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class Key, class T, typename Allocator>
unordered_map(
std::initializer_list<std::pair<const Key, T> >, std::size_t, Allocator)
->unordered_map<Key, T, boost::hash<Key>, std::equal_to<Key>, Allocator>;
template <class Key, class T, typename Allocator>
unordered_map(std::initializer_list<std::pair<const Key, T> >, Allocator)
->unordered_map<Key, T, boost::hash<Key>, std::equal_to<Key>, Allocator>;
template <class Key, class T, class Hash, class Allocator>
unordered_map(std::initializer_list<std::pair<const Key, T> >, std::size_t,
Hash, Allocator)
->unordered_map<Key, T, Hash, std::equal_to<Key>, Allocator>;
#endif
template <class K, class T, class H, class P, class A>
class unordered_multimap
{
@ -1363,6 +1438,73 @@ namespace boost {
#endif
}; // class template unordered_multimap
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
template <class InputIterator,
class Hash =
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
class Pred =
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
class Allocator = std::allocator<
boost::unordered::detail::iter_to_alloc_t<InputIterator> > >
unordered_multimap(InputIterator, InputIterator,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_multimap<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>, Hash, Pred,
Allocator>;
template <class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T> > >
unordered_multimap(std::initializer_list<std::pair<const Key, T> >,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_multimap<Key, T, Hash, Pred, Allocator>;
template <class InputIterator, class Allocator>
unordered_multimap(InputIterator, InputIterator, std::size_t, Allocator)
->unordered_multimap<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>,
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class InputIterator, class Allocator>
unordered_multimap(InputIterator, InputIterator, Allocator)
->unordered_multimap<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>,
boost::hash<boost::unordered::detail::iter_key_t<InputIterator> >,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class InputIterator, class Hash, class Allocator>
unordered_multimap(
InputIterator, InputIterator, std::size_t, Hash, Allocator)
->unordered_multimap<boost::unordered::detail::iter_key_t<InputIterator>,
boost::unordered::detail::iter_val_t<InputIterator>, Hash,
std::equal_to<boost::unordered::detail::iter_key_t<InputIterator> >,
Allocator>;
template <class Key, class T, typename Allocator>
unordered_multimap(
std::initializer_list<std::pair<const Key, T> >, std::size_t, Allocator)
->unordered_multimap<Key, T, boost::hash<Key>, std::equal_to<Key>,
Allocator>;
template <class Key, class T, typename Allocator>
unordered_multimap(
std::initializer_list<std::pair<const Key, T> >, Allocator)
->unordered_multimap<Key, T, boost::hash<Key>, std::equal_to<Key>,
Allocator>;
template <class Key, class T, class Hash, class Allocator>
unordered_multimap(std::initializer_list<std::pair<const Key, T> >,
std::size_t, Hash, Allocator)
->unordered_multimap<Key, T, Hash, std::equal_to<Key>, Allocator>;
#endif
////////////////////////////////////////////////////////////////////////////
template <class K, class T, class H, class P, class A>

View File

@ -545,6 +545,52 @@ namespace boost {
#endif
}; // class template unordered_set
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
template <class InputIterator,
class Hash =
boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
class Pred =
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
class Allocator = std::allocator<
typename std::iterator_traits<InputIterator>::value_type> >
unordered_set(InputIterator, InputIterator,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_set<typename std::iterator_traits<InputIterator>::value_type,
Hash, Pred, Allocator>;
template <class T, class Hash = boost::hash<T>,
class Pred = std::equal_to<T>, class Allocator = std::allocator<T> >
unordered_set(std::initializer_list<T>,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_set<T, Hash, Pred, Allocator>;
template <class InputIterator, class Allocator>
unordered_set(InputIterator, InputIterator, std::size_t, Allocator)
->unordered_set<typename std::iterator_traits<InputIterator>::value_type,
boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
Allocator>;
template <class InputIterator, class Hash, class Allocator>
unordered_set(InputIterator, InputIterator, std::size_t, Hash, Allocator)
->unordered_set<typename std::iterator_traits<InputIterator>::value_type,
Hash,
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
Allocator>;
template <class T, class Allocator>
unordered_set(std::initializer_list<T>, std::size_t, Allocator)
->unordered_set<T, boost::hash<T>, std::equal_to<T>, Allocator>;
template <class T, class Hash, class Allocator>
unordered_set(std::initializer_list<T>, std::size_t, Hash, Allocator)
->unordered_set<T, Hash, std::equal_to<T>, Allocator>;
#endif
template <class T, class H, class P, class A> class unordered_multiset
{
#if defined(BOOST_UNORDERED_USE_MOVE)
@ -1049,6 +1095,55 @@ namespace boost {
#endif
}; // class template unordered_multiset
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
template <class InputIterator,
class Hash =
boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
class Pred =
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
class Allocator = std::allocator<
typename std::iterator_traits<InputIterator>::value_type> >
unordered_multiset(InputIterator, InputIterator,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_multiset<
typename std::iterator_traits<InputIterator>::value_type, Hash, Pred,
Allocator>;
template <class T, class Hash = boost::hash<T>,
class Pred = std::equal_to<T>, class Allocator = std::allocator<T> >
unordered_multiset(std::initializer_list<T>,
std::size_t = boost::unordered::detail::default_bucket_count,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
->unordered_multiset<T, Hash, Pred, Allocator>;
template <class InputIterator, class Allocator>
unordered_multiset(InputIterator, InputIterator, std::size_t, Allocator)
->unordered_multiset<
typename std::iterator_traits<InputIterator>::value_type,
boost::hash<typename std::iterator_traits<InputIterator>::value_type>,
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
Allocator>;
template <class InputIterator, class Hash, class Allocator>
unordered_multiset(
InputIterator, InputIterator, std::size_t, Hash, Allocator)
->unordered_multiset<
typename std::iterator_traits<InputIterator>::value_type, Hash,
std::equal_to<typename std::iterator_traits<InputIterator>::value_type>,
Allocator>;
template <class T, class Allocator>
unordered_multiset(std::initializer_list<T>, std::size_t, Allocator)
->unordered_multiset<T, boost::hash<T>, std::equal_to<T>, Allocator>;
template <class T, class Hash, class Allocator>
unordered_multiset(std::initializer_list<T>, std::size_t, Hash, Allocator)
->unordered_multiset<T, Hash, std::equal_to<T>, Allocator>;
#endif
////////////////////////////////////////////////////////////////////////////
template <class T, class H, class P, class A>
unordered_set<T, H, P, A>::unordered_set()

View File

@ -63,6 +63,7 @@ test-suite unordered
[ run unordered/equality_tests.cpp ]
[ run unordered/swap_tests.cpp ]
[ run unordered/detail_tests.cpp ]
[ run unordered/deduction_tests.cpp ]
[ run unordered/compile_set.cpp : :
: <define>BOOST_UNORDERED_USE_MOVE

View File

@ -0,0 +1,347 @@
#include <boost/unordered_map.hpp>
#include <iostream>
#include <vector>
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
struct hash_equals
{
template <typename T> bool operator()(T const& x) const
{
boost::hash<T> hf;
return hf(x);
}
template <typename T> bool operator()(T const& x, T const& y) const
{
std::equal_to<T> eq;
return eq(x, y);
}
};
template <typename T> struct test_allocator
{
typedef T value_type;
test_allocator() = default;
template <typename T2> test_allocator(test_allocator<T2> const&) {}
T* allocate(std::size_t n) const { return (T*)malloc(sizeof(T) * n); }
void deallocate(T* ptr, std::size_t) const { free(ptr); }
bool operator==(test_allocator const&) { return true; }
bool operator!=(test_allocator const&) { return false; }
};
#endif
int main()
{
std::cout << "BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES: "
<< BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES << std::endl;
#if BOOST_UNORDERED_TEMPLATE_DEDUCTION_GUIDES
std::vector<std::pair<int, int> > x;
x.push_back(std::make_pair(1, 3));
x.push_back(std::make_pair(5, 10));
test_allocator<std::pair<const int, int> > pair_allocator;
hash_equals f;
// unordered_map
/*
template<class InputIterator,
class Hash = hash<iter_key_t<InputIterator>>,
class Pred = equal_to<iter_key_t<InputIterator>>,
class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
unordered_map(InputIterator, InputIterator, typename see below::size_type =
see below,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
-> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
Hash, Pred,
Allocator>;
*/
{
boost::unordered_map m(x.begin(), x.end());
static_assert(
std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
}
/* Ambiguous:
{
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>>>::value);
}
{
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>, std::equal_to<int>>>::value);
}
*/
{
boost::unordered_map m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>(), pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
/*
template<class Key, class T, class Hash = hash<Key>,
class Pred = equal_to<Key>, class Allocator = allocator<pair<const
Key, T>>>
unordered_map(initializer_list<pair<const Key, T>>,
typename see below::size_type = see below, Hash = Hash(),
Pred = Pred(), Allocator = Allocator())
-> unordered_map<Key, T, Hash, Pred, Allocator>;
*/
{
boost::unordered_map m({std::pair<int const, int>(1, 2)});
static_assert(
std::is_same<decltype(m), boost::unordered_map<int, int> >::value);
}
/* Ambiguous
{
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>>>::value);
}
{
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>(), std::equal_to<int>());
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
std::hash<int>, std::equal_to<int>>>::value);
}
*/
{
boost::unordered_map m(
{std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_map<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > > >::value);
}
/*
template<class InputIterator, class Allocator>
unordered_map(InputIterator, InputIterator, typename see below::size_type,
Allocator)
-> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
hash<iter_key_t<InputIterator>>,
equal_to<iter_key_t<InputIterator>>,
Allocator>;
*/
/* Ambiguous
{
boost::unordered_map m(x.begin(), x.end(), 0u, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/*
template<class InputIterator, class Allocator>
unordered_map(InputIterator, InputIterator, Allocator)
-> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
hash<iter_key_t<InputIterator>>,
equal_to<iter_key_t<InputIterator>>,
Allocator>;
*/
/* No constructor:
{
boost::unordered_map m(x.begin(), x.end(), pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/*
template<class InputIterator, class Hash, class Allocator>
unordered_map(InputIterator, InputIterator, typename see below::size_type,
Hash, Allocator)
-> unordered_map<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
Hash,
equal_to<iter_key_t<InputIterator>>, Allocator>;
*/
/* Ambiguous
{
boost::unordered_map m(x.begin(), x.end(), 0u, f, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_map<int, int,
hash_equals, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/*
template<class Key, class T, typename Allocator>
unordered_map(initializer_list<pair<const Key, T>>, typename see
below::size_type,
Allocator)
-> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>;
*/
/* Ambiguous
{
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/*
template<class Key, class T, typename Allocator>
unordered_map(initializer_list<pair<const Key, T>>, Allocator)
-> unordered_map<Key, T, hash<Key>, equal_to<Key>, Allocator>;
*/
{
boost::unordered_map m({std::pair<int const, int>(1, 2)}, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_map<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
/*
template<class Key, class T, class Hash, class Allocator>
unordered_map(initializer_list<pair<const Key, T>>, typename see
below::size_type, Hash,
Allocator)
-> unordered_map<Key, T, Hash, equal_to<Key>, Allocator>;
*/
/* Ambiguous
{
boost::unordered_map m({std::pair<int const, int>(1,2)}, 0, f,
pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_map<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
// unordered_multimap
{
boost::unordered_multimap m(x.begin(), x.end());
static_assert(
std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
}
/* Ambiguous:
{
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>>>::value);
}
{
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>, std::equal_to<int>>>::value);
}
*/
{
boost::unordered_multimap m(x.begin(), x.end(), 0, std::hash<int>(),
std::equal_to<int>(), pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, std::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1, 2)});
static_assert(
std::is_same<decltype(m), boost::unordered_multimap<int, int> >::value);
}
/* Ambiguous
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>>>::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
std::hash<int>(), std::equal_to<int>());
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
std::hash<int>, std::equal_to<int>>>::value);
}
*/
{
boost::unordered_multimap m(
{std::pair<int const, int>(1, 2)}, 0, f, f, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, hash_equals, hash_equals,
test_allocator<std::pair<const int, int> > > >::value);
}
/* Ambiguous
{
boost::unordered_multimap m(x.begin(), x.end(), 0u, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/* No constructor:
{
boost::unordered_multimap m(x.begin(), x.end(), pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
/* Ambiguous
{
boost::unordered_multimap m(x.begin(), x.end(), 0u, f, pair_allocator);
static_assert(std::is_same<decltype(m), boost::unordered_multimap<int, int,
hash_equals, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0,
pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
{
boost::unordered_multimap m(
{std::pair<int const, int>(1, 2)}, pair_allocator);
static_assert(std::is_same<decltype(m),
boost::unordered_multimap<int, int, boost::hash<int>, std::equal_to<int>,
test_allocator<std::pair<const int, int> > > >::value);
}
/* Ambiguous
{
boost::unordered_multimap m({std::pair<int const, int>(1,2)}, 0, f,
pair_allocator);
static_assert(std::is_same<decltype(m),boost::unordered_multimap<int, int,
boost::hash<int>, std::equal_to<int>, test_allocator<std::pair<const int,
int>>>>::value);
}
*/
#endif
}