Use C++14 index_sequence when possible

Newer standard libraries use compiler intrinsics for std::index_sequence
which makes it ‘free’. This prevents hitting instantiation limits for
recursive templates (-ftemplate-depth).
This commit is contained in:
Dean Moldovan 2016-11-27 20:56:04 +01:00 committed by Wenzel Jakob
parent 107285b353
commit 8c85a85747
4 changed files with 17 additions and 11 deletions

View File

@ -774,7 +774,7 @@ protected:
template <typename... Tuple> class type_caster<std::tuple<Tuple...>> { template <typename... Tuple> class type_caster<std::tuple<Tuple...>> {
using type = std::tuple<Tuple...>; using type = std::tuple<Tuple...>;
using indices = typename make_index_sequence<sizeof...(Tuple)>::type; using indices = make_index_sequence<sizeof...(Tuple)>;
static constexpr auto size = sizeof...(Tuple); static constexpr auto size = sizeof...(Tuple);
public: public:
@ -1205,7 +1205,7 @@ NAMESPACE_BEGIN(detail)
template <typename... Args> template <typename... Args>
class argument_loader { class argument_loader {
using itypes = type_list<intrinsic_t<Args>...>; using itypes = type_list<intrinsic_t<Args>...>;
using indices = typename make_index_sequence<sizeof...(Args)>::type; using indices = make_index_sequence<sizeof...(Args)>;
public: public:
static constexpr auto has_kwargs = std::is_same<itypes, type_list<args, kwargs>>::value; static constexpr auto has_kwargs = std::is_same<itypes, type_list<args, kwargs>>::value;

View File

@ -346,9 +346,15 @@ struct internals {
inline internals &get_internals(); inline internals &get_internals();
/// Index sequence for convenient template metaprogramming involving tuples /// Index sequence for convenient template metaprogramming involving tuples
#ifdef PYBIND11_CPP14
using std::index_sequence;
using std::make_index_sequence;
#else
template<size_t ...> struct index_sequence { }; template<size_t ...> struct index_sequence { };
template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { }; template<size_t N, size_t ...S> struct make_index_sequence_impl : make_index_sequence_impl <N - 1, N - 1, S...> { };
template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; }; template<size_t ...S> struct make_index_sequence_impl <0, S...> { typedef index_sequence<S...> type; };
template<size_t N> using make_index_sequence = typename make_index_sequence_impl<N>::type;
#endif
/// Strip the class from a method type /// Strip the class from a method type
template <typename T> struct remove_class { }; template <typename T> struct remove_class { };

View File

@ -22,8 +22,8 @@ template <size_t Size1, size_t Size2> class descr {
public: public:
constexpr descr(char const (&text) [Size1+1], const std::type_info * const (&types)[Size2+1]) constexpr descr(char const (&text) [Size1+1], const std::type_info * const (&types)[Size2+1])
: descr(text, types, : descr(text, types,
typename make_index_sequence<Size1>::type(), make_index_sequence<Size1>(),
typename make_index_sequence<Size2>::type()) { } make_index_sequence<Size2>()) { }
constexpr const char *text() const { return m_text; } constexpr const char *text() const { return m_text; }
constexpr const std::type_info * const * types() const { return m_types; } constexpr const std::type_info * const * types() const { return m_types; }
@ -31,10 +31,10 @@ public:
template <size_t OtherSize1, size_t OtherSize2> template <size_t OtherSize1, size_t OtherSize2>
constexpr descr<Size1 + OtherSize1, Size2 + OtherSize2> operator+(const descr<OtherSize1, OtherSize2> &other) const { constexpr descr<Size1 + OtherSize1, Size2 + OtherSize2> operator+(const descr<OtherSize1, OtherSize2> &other) const {
return concat(other, return concat(other,
typename make_index_sequence<Size1>::type(), make_index_sequence<Size1>(),
typename make_index_sequence<Size2>::type(), make_index_sequence<Size2>(),
typename make_index_sequence<OtherSize1>::type(), make_index_sequence<OtherSize1>(),
typename make_index_sequence<OtherSize2>::type()); make_index_sequence<OtherSize2>());
} }
protected: protected:

View File

@ -1077,7 +1077,7 @@ struct vectorize_helper {
explicit vectorize_helper(T&&f) : f(std::forward<T>(f)) { } explicit vectorize_helper(T&&f) : f(std::forward<T>(f)) { }
object operator()(array_t<Args, array::c_style | array::forcecast>... args) { object operator()(array_t<Args, array::c_style | array::forcecast>... args) {
return run(args..., typename make_index_sequence<sizeof...(Args)>::type()); return run(args..., make_index_sequence<sizeof...(Args)>());
} }
template <size_t ... Index> object run(array_t<Args, array::c_style | array::forcecast>&... args, index_sequence<Index...> index) { template <size_t ... Index> object run(array_t<Args, array::c_style | array::forcecast>&... args, index_sequence<Index...> index) {