Skip to content

Commit

Permalink
Fix compilation with avr-gcc 7
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Sep 11, 2018
1 parent 44199da commit 1e245c1
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 0 deletions.
234 changes: 234 additions & 0 deletions include/type_traits
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,221 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __is_default_constructible_safe<_Tp>::type
{ };

#if __GNUC__ >= 8
/// is_constructible
template<typename _Tp, typename... _Args>
struct is_constructible
: public __bool_constant<__is_constructible(_Tp, _Args...)>
{ };
#else
// Implementation of is_constructible.

// The hardest part of this trait is the binary direct-initialization
// case, because we hit into a functional cast of the form T(arg).
// This implementation uses different strategies depending on the
// target type to reduce the test overhead as much as possible:
//
// a) For a reference target type, we use a static_cast expression
// modulo its extra cases.
//
// b) For a non-reference target type we use a ::new expression.
struct __do_is_static_castable_impl
{
template<typename _From, typename _To, typename
= decltype(static_cast<_To>(declval<_From>()))>
static true_type __test(int);

template<typename, typename>
static false_type __test(...);
};

template<typename _From, typename _To>
struct __is_static_castable_impl
: public __do_is_static_castable_impl
{
typedef decltype(__test<_From, _To>(0)) type;
};

template<typename _From, typename _To>
struct __is_static_castable_safe
: public __is_static_castable_impl<_From, _To>::type
{ };

// __is_static_castable
template<typename _From, typename _To>
struct __is_static_castable
: public integral_constant<bool, (__is_static_castable_safe<
_From, _To>::value)>
{ };

// Implementation for non-reference types. To meet the proper
// variable definition semantics, we also need to test for
// is_destructible in this case.
// This form should be simplified by a single expression:
// ::delete ::new _Tp(declval<_Arg>()), see c++/51222.
struct __do_is_direct_constructible_impl
{
template<typename _Tp, typename _Arg, typename
= decltype(::new _Tp(declval<_Arg>()))>
static true_type __test(int);

template<typename, typename>
static false_type __test(...);
};

template<typename _Tp, typename _Arg>
struct __is_direct_constructible_impl
: public __do_is_direct_constructible_impl
{
typedef decltype(__test<_Tp, _Arg>(0)) type;
};

template<typename _Tp, typename _Arg>
struct __is_direct_constructible_new_safe
: public __and_<is_destructible<_Tp>,
__is_direct_constructible_impl<_Tp, _Arg>>
{ };

template<typename, typename>
struct is_same;

template<typename, typename>
struct is_base_of;

template<typename>
struct remove_reference;

template<typename _From, typename _To, bool
= __not_<__or_<is_void<_From>,
is_function<_From>>>::value>
struct __is_base_to_derived_ref;

template<typename _Tp, typename... _Args>
struct is_constructible;

// Detect whether we have a downcast situation during
// reference binding.
template<typename _From, typename _To>
struct __is_base_to_derived_ref<_From, _To, true>
{
typedef typename remove_cv<typename remove_reference<_From
>::type>::type __src_t;
typedef typename remove_cv<typename remove_reference<_To
>::type>::type __dst_t;
typedef __and_<__not_<is_same<__src_t, __dst_t>>,
is_base_of<__src_t, __dst_t>,
__not_<is_constructible<__dst_t, _From>>> type;
static constexpr bool value = type::value;
};

template<typename _From, typename _To>
struct __is_base_to_derived_ref<_From, _To, false>
: public false_type
{ };

template<typename _From, typename _To, bool
= __and_<is_lvalue_reference<_From>,
is_rvalue_reference<_To>>::value>
struct __is_lvalue_to_rvalue_ref;

// Detect whether we have an lvalue of non-function type
// bound to a reference-compatible rvalue-reference.
template<typename _From, typename _To>
struct __is_lvalue_to_rvalue_ref<_From, _To, true>
{
typedef typename remove_cv<typename remove_reference<
_From>::type>::type __src_t;
typedef typename remove_cv<typename remove_reference<
_To>::type>::type __dst_t;
typedef __and_<__not_<is_function<__src_t>>,
__or_<is_same<__src_t, __dst_t>,
is_base_of<__dst_t, __src_t>>> type;
static constexpr bool value = type::value;
};

template<typename _From, typename _To>
struct __is_lvalue_to_rvalue_ref<_From, _To, false>
: public false_type
{ };

// Here we handle direct-initialization to a reference type as
// equivalent to a static_cast modulo overshooting conversions.
// These are restricted to the following conversions:
// a) A base class value to a derived class reference
// b) An lvalue to an rvalue-reference of reference-compatible
// types that are not functions
template<typename _Tp, typename _Arg>
struct __is_direct_constructible_ref_cast
: public __and_<__is_static_castable<_Arg, _Tp>,
__not_<__or_<__is_base_to_derived_ref<_Arg, _Tp>,
__is_lvalue_to_rvalue_ref<_Arg, _Tp>
>>>
{ };

template<typename _Tp, typename _Arg>
struct __is_direct_constructible_new
: public conditional<is_reference<_Tp>::value,
__is_direct_constructible_ref_cast<_Tp, _Arg>,
__is_direct_constructible_new_safe<_Tp, _Arg>
>::type
{ };

template<typename _Tp, typename _Arg>
struct __is_direct_constructible
: public __is_direct_constructible_new<_Tp, _Arg>::type
{ };

// Since default-construction and binary direct-initialization have
// been handled separately, the implementation of the remaining
// n-ary construction cases is rather straightforward. We can use
// here a functional cast, because array types are excluded anyway
// and this form is never interpreted as a C cast.
struct __do_is_nary_constructible_impl
{
template<typename _Tp, typename... _Args, typename
= decltype(_Tp(declval<_Args>()...))>
static true_type __test(int);

template<typename, typename...>
static false_type __test(...);
};

template<typename _Tp, typename... _Args>
struct __is_nary_constructible_impl
: public __do_is_nary_constructible_impl
{
typedef decltype(__test<_Tp, _Args...>(0)) type;
};

template<typename _Tp, typename... _Args>
struct __is_nary_constructible
: public __is_nary_constructible_impl<_Tp, _Args...>::type
{
static_assert(sizeof...(_Args) > 1,
"Only useful for > 1 arguments");
};

template<typename _Tp, typename... _Args>
struct __is_constructible_impl
: public __is_nary_constructible<_Tp, _Args...>
{ };

template<typename _Tp, typename _Arg>
struct __is_constructible_impl<_Tp, _Arg>
: public __is_direct_constructible<_Tp, _Arg>
{ };

template<typename _Tp>
struct __is_constructible_impl<_Tp>
: public is_default_constructible<_Tp>
{ };

/// is_constructible
template<typename _Tp, typename... _Args>
struct is_constructible
: public __is_constructible_impl<_Tp, _Args...>::type
{ };
#endif

template<typename _Tp, bool = __is_referenceable<_Tp>::value>
struct __is_copy_constructible_impl;
Expand Down Expand Up @@ -1043,11 +1253,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __is_nothrow_move_constructible_impl<_Tp>
{ };

#if __GNUC__ >= 8
/// is_assignable
template<typename _Tp, typename _Up>
struct is_assignable
: public __bool_constant<__is_assignable(_Tp, _Up)>
{ };
#else
template<typename _Tp, typename _Up>
class __is_assignable_helper
{
template<typename _Tp1, typename _Up1,
typename = decltype(declval<_Tp1>() = declval<_Up1>())>
static true_type
__test(int);

template<typename, typename>
static false_type
__test(...);

public:
typedef decltype(__test<_Tp, _Up>(0)) type;
};

/// is_assignable
template<typename _Tp, typename _Up>
struct is_assignable
: public __is_assignable_helper<_Tp, _Up>::type
{ };
#endif

template<typename _Tp, bool = __is_referenceable<_Tp>::value>
struct __is_copy_assignable_impl;
Expand Down
60 changes: 60 additions & 0 deletions include/utility
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// extract the elements in a tuple.
template<size_t... _Indexes> struct _Index_tuple { };

#if __GNUC__ >= 8 || defined(_GLIBCXX_USE_MAKE_INTEGER_SEQ)

#ifdef __has_builtin
# if __has_builtin(__make_integer_seq)
# define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
Expand All @@ -307,11 +309,41 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
using __type = _Index_tuple<__integer_pack(_Num)...>;
#endif
};
#else
// Concatenates two _Index_tuples.
template<typename _Itup1, typename _Itup2> struct _Itup_cat;

template<size_t... _Ind1, size_t... _Ind2>
struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>>
{
using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>;
};

// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
template<size_t _Num>
struct _Build_index_tuple
: _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type,
typename _Build_index_tuple<_Num - _Num / 2>::__type>
{ };

template<>
struct _Build_index_tuple<1>
{
typedef _Index_tuple<0> __type;
};

template<>
struct _Build_index_tuple<0>
{
typedef _Index_tuple<> __type;
};
#endif // __GNUC__ >= 8 || defined(_GLIBCXX_USE_MAKE_INTEGER_SEQ)

#if __cplusplus > 201103L

#define __cpp_lib_integer_sequence 201304

#if __GNUC__ >= 8 || defined(_GLIBCXX_USE_MAKE_INTEGER_SEQ)
/// Class template integer_sequence
template<typename _Tp, _Tp... _Idx>
struct integer_sequence
Expand All @@ -331,6 +363,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION

#undef _GLIBCXX_USE_MAKE_INTEGER_SEQ

#else
/// Class template integer_sequence
template<typename _Tp, _Tp... _Idx>
struct integer_sequence
{
typedef _Tp value_type;
static constexpr size_t size() { return sizeof...(_Idx); }
};

template<typename _Tp, _Tp _Num,
typename _ISeq = typename _Build_index_tuple<_Num>::__type>
struct _Make_integer_sequence;

template<typename _Tp, _Tp _Num, size_t... _Idx>
struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
{
static_assert( _Num >= 0,
"Cannot make integer sequence of negative length" );

typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
};

/// Alias template make_integer_sequence
template<typename _Tp, _Tp _Num>
using make_integer_sequence
= typename _Make_integer_sequence<_Tp, _Num>::__type;
#endif // __GNUC__ >= 8 || defined(_GLIBCXX_USE_MAKE_INTEGER_SEQ)

/// Alias template index_sequence
template<size_t... _Idx>
using index_sequence = integer_sequence<size_t, _Idx...>;
Expand Down

0 comments on commit 1e245c1

Please sign in to comment.