Skip to content

Commit

Permalink
Added etl::is_default_constructible
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Mar 6, 2024
1 parent 95af36a commit e1d563a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/etl/generators/type_traits_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,11 @@ typedef integral_constant<bool, true> true_type;
template<typename T, typename... TArgs>
using is_constructible = std::is_constructible<T, TArgs...>;

//*********************************************
// is_default_constructible
template<typename T, typename... TArgs>
using is_default_constructible = std::is_default_constructible<T>;

//*********************************************
// is_copy_constructible
template <typename T>
Expand Down Expand Up @@ -1879,6 +1884,14 @@ typedef integral_constant<bool, true> true_type;
};
#endif

//*********************************************
// is_default_constructible
template<typename T, typename = void>
struct is_default_constructible : etl::false_type { };

template<typename T>
struct is_default_constructible<T, etl::void_t<decltype(T())>> : etl::true_type { };

//*********************************************
// is_copy_constructible
template <typename T, bool B = etl::is_arithmetic<T>::value || etl::is_pointer<T>::value>
Expand Down
13 changes: 13 additions & 0 deletions include/etl/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,11 @@ typedef integral_constant<bool, true> true_type;
template<typename T, typename... TArgs>
using is_constructible = std::is_constructible<T, TArgs...>;

//*********************************************
// is_default_constructible
template<typename T, typename... TArgs>
using is_default_constructible = std::is_default_constructible<T>;

//*********************************************
// is_copy_constructible
template <typename T>
Expand Down Expand Up @@ -1872,6 +1877,14 @@ typedef integral_constant<bool, true> true_type;
};
#endif

//*********************************************
// is_default_constructible
template<typename T, typename = void>
struct is_default_constructible : etl::false_type { };

template<typename T>
struct is_default_constructible<T, etl::void_t<decltype(T())>> : etl::true_type { };

//*********************************************
// is_copy_constructible
template <typename T, bool B = etl::is_arithmetic<T>::value || etl::is_pointer<T>::value>
Expand Down
29 changes: 29 additions & 0 deletions test/test_type_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ namespace
MoveableCopyable(const MoveableCopyable&) {}
MoveableCopyable& operator =(const MoveableCopyable&) { return *this; }
};

//*********************************************
struct NotDefaultConstructible
{
NotDefaultConstructible() = delete;
NotDefaultConstructible(const NotDefaultConstructible&) noexcept {}
NotDefaultConstructible& operator =(const NotDefaultConstructible&) noexcept { return *this; }

NotDefaultConstructible(NotDefaultConstructible&&) = delete;
NotDefaultConstructible& operator =(NotDefaultConstructible&) = delete;
};
}

// Definitions for when the STL and compiler built-ins are not available.
Expand Down Expand Up @@ -1137,10 +1148,28 @@ namespace
CHECK((etl::is_constructible_v<Copyable>) == (std::is_constructible_v<Copyable>));
CHECK((etl::is_constructible_v<Moveable>) == (std::is_constructible_v<Moveable>));
CHECK((etl::is_constructible_v<MoveableCopyable>) == (std::is_constructible_v<MoveableCopyable>));
CHECK((etl::is_constructible_v<NotDefaultConstructible>) == (std::is_constructible_v<NotDefaultConstructible>));
#else
CHECK((etl::is_constructible<Copyable>::value) == (std::is_constructible<Copyable>::value));
CHECK((etl::is_constructible<Moveable>::value) == (std::is_constructible<Moveable>::value));
CHECK((etl::is_constructible<MoveableCopyable>::value) == (std::is_constructible<MoveableCopyable>::value));
CHECK((etl::is_constructible<NotDefaultConstructible>::value) == (std::is_constructible<NotDefaultConstructible>::value));
#endif
}

//*************************************************************************
TEST(test_is_default_constructible)
{
#if ETL_USING_CPP17
CHECK((etl::is_default_constructible_v<Copyable>) == (std::is_default_constructible_v<Copyable>));
CHECK((etl::is_default_constructible_v<Moveable>) == (std::is_default_constructible_v<Moveable>));
CHECK((etl::is_default_constructible_v<MoveableCopyable>) == (std::is_default_constructible_v<MoveableCopyable>));
CHECK((etl::is_default_constructible_v<NotDefaultConstructible>) == (std::is_default_constructible_v<NotDefaultConstructible>));
#else
CHECK((etl::is_default_constructible<Copyable>::value) == (std::is_default_constructible<Copyable>::value));
CHECK((etl::is_default_constructible<Moveable>::value) == (std::is_default_constructible<Moveable>::value));
CHECK((etl::is_default_constructible<MoveableCopyable>::value) == (std::is_default_constructible<MoveableCopyable>::value));
CHECK((etl::is_default_constructible<NotDefaultConstructible>::value) == (std::is_default_constructible<NotDefaultConstructible>::value));
#endif
}

Expand Down

0 comments on commit e1d563a

Please sign in to comment.