Index: libcxx/CREDITS.TXT =================================================================== --- libcxx/CREDITS.TXT +++ libcxx/CREDITS.TXT @@ -41,6 +41,11 @@ E: jbcoe@me.com D: Implementation of propagate_const. +N: Christopher Di Bella +E: cjdb@google.com +E: cjdb.ns@gmail.com +D: Library concepts. + N: Glen Joseph Fernandes E: glenjofe@gmail.com D: Implementation of to_address. Index: libcxx/include/concepts =================================================================== --- /dev/null +++ libcxx/include/concepts @@ -0,0 +1,164 @@ +// -*- C++ -*- +//===-------------------------- concepts -----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_CONCEPTS +#define _LIBCPP_CONCEPTS + +/* + concepts synopsis +namespace std { + // [concepts.lang], language-related concepts + // [concept.same], concept same_as + template<class T, class U> + concept same_as = see below; + + // [concept.derived], concept derived_from + template<class Derived, class Base> + concept derived_from = see below; + + // [concept.convertible], concept convertible_to + template<class From, class To> + concept convertible_to = see below; + + // [concept.commonref], concept common_reference_with + template<class T, class U> + concept common_reference_with = see below; + + // [concept.common], concept common_with + template<class T, class U> + concept common_with = see below; + + // [concepts.arithmetic], arithmetic concepts + template<class T> + concept integral = see below; + template<class T> + concept signed_integral = see below; + template<class T> + concept unsigned_integral = see below; + template<class T> + concept floating_point = see below; + + // [concept.assignable], concept assignable_from + template<class LHS, class RHS> + concept assignable_from = see below; + + // [concept.swappable], concept swappable + namespace ranges { + inline namespace unspecified { + inline constexpr unspecified swap = unspecified; + } + } + template<class T> + concept swappable = see below; + template<class T, class U> + concept swappable_with = see below; + + // [concept.destructible], concept destructible + template<class T> + concept destructible = see below; + + // [concept.constructible], concept constructible_from + template<class T, class... Args> + concept constructible_from = see below; + + // [concept.defaultconstructible], concept default_constructible + template<class T> + concept default_constructible = see below; + + // [concept.moveconstructible], concept move_constructible + template<class T> + concept move_constructible = see below; + + // [concept.copyconstructible], concept copy_constructible + template<class T> + concept copy_constructible = see below; + + // [concepts.compare], comparison concepts + // [concept.boolean], concept boolean + template<class B> + concept boolean = see below; + + // [concept.equalitycomparable], concept equality_comparable + template<class T> + concept equality_comparable = see below; + template<class T, class U> + concept equality_comparable_with = see below; + + // [concept.totallyordered], concept totally_ordered + template<class T> + concept totally_ordered = see below; + template<class T, class U> + concept totally_ordered_with = see below; + + // [concepts.object], object concepts + template<class T> + concept movable = see below; + template<class T> + concept copyable = see below; + template<class T> + concept semiregular = see below; + template<class T> + concept regular = see below; + + // [concepts.callable], callable concepts + // [concept.invocable], concept invocable + template<class F, class... Args> + concept invocable = see below; + + // [concept.regularinvocable], concept regular_invocable + template<class F, class... Args> + concept regular_invocable = see below; + + // [concept.predicate], concept predicate + template<class F, class... Args> + concept predicate = see below; + + // [concept.relation], concept relation + template<class R, class T, class U> + concept relation = see below; + + // [concept.equiv], concept equivalence_relation + template<class R, class T, class U> + concept equivalence_relation = see below; + + // [concept.strictweakorder], concept strict_weak_order + template<class R, class T, class U> + concept strict_weak_order = see below; +} + +*/ + +#include <__config> +#include <type_traits> + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 + +// [concept.same] +template <class T, class U> +concept __same_as_impl = __is_same(T, U); + +template <class T, class U> +concept same_as = __same_as_impl<T, U> && __same_as_impl<U, T>; + +// [concept.derived] +template<class Derived, class Base> +concept derived_from = + is_base_of_v<Base, Derived> && + is_convertible_v<const volatile Derived*, const volatile Base*>; + +#endif //_LIBCPP_STD_VER > 17 + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_CONCEPTS Index: libcxx/test/std/concepts/lang/same_as.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/concepts/lang/same_as.pass.cpp @@ -0,0 +1,190 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include <concepts> + +struct S1 {}; +struct S2 { + int i; +}; +struct S3 { + int& r; +}; +struct S4 { + int&& r; +}; +struct S5 { + int* p; +}; + +class C1 {}; +class C2 { + [[maybe_unused]] int i; +}; + +class C3 { +public: + int i; +}; + +template <class T1, class T2 = T1> +class C4 { + int t1; + int t2; +}; + +template <class T1, class T2 = T1> +class C5 { + [[maybe_unused]] T1 t1; + +public: + T2 t2; +}; + +template <class T1, class T2 = T1> +class C6 { +public: + [[maybe_unused]] T1 t1; + [[maybe_unused]] T2 t2; +}; + +template <class T> +struct identity { + using type = T; +}; + +template <template <typename> class Modifier = identity> +void CheckSameAs() { + static_assert( + std::same_as<typename Modifier<int>::type, typename Modifier<int>::type>); + static_assert( + std::same_as<typename Modifier<S1>::type, typename Modifier<S1>::type>); + static_assert( + std::same_as<typename Modifier<S2>::type, typename Modifier<S2>::type>); + static_assert( + std::same_as<typename Modifier<S3>::type, typename Modifier<S3>::type>); + static_assert( + std::same_as<typename Modifier<S4>::type, typename Modifier<S4>::type>); + static_assert( + std::same_as<typename Modifier<S5>::type, typename Modifier<S5>::type>); + static_assert( + std::same_as<typename Modifier<C1>::type, typename Modifier<C1>::type>); + static_assert( + std::same_as<typename Modifier<C2>::type, typename Modifier<C2>::type>); + static_assert( + std::same_as<typename Modifier<C3>::type, typename Modifier<C3>::type>); + static_assert(std::same_as<typename Modifier<C4<int> >::type, + typename Modifier<C4<int> >::type>); + static_assert(std::same_as<typename Modifier<C4<int&> >::type, + typename Modifier<C4<int&> >::type>); + static_assert(std::same_as<typename Modifier<C4<int&&> >::type, + typename Modifier<C4<int&&> >::type>); + static_assert(std::same_as<typename Modifier<C5<int> >::type, + typename Modifier<C5<int> >::type>); + static_assert(std::same_as<typename Modifier<C5<int&> >::type, + typename Modifier<C5<int&> >::type>); + static_assert(std::same_as<typename Modifier<C5<int&&> >::type, + typename Modifier<C5<int&&> >::type>); + static_assert(std::same_as<typename Modifier<C6<int> >::type, + typename Modifier<C6<int> >::type>); + static_assert(std::same_as<typename Modifier<C6<int&> >::type, + typename Modifier<C6<int&> >::type>); + static_assert(std::same_as<typename Modifier<C6<int&&> >::type, + typename Modifier<C6<int&&> >::type>); +} + +template <template <typename> class Modifier1, + template <typename> class Modifier2 = identity> +void CheckNotSameAs() { + static_assert(!std::same_as<typename Modifier1<int>::type, + typename Modifier2<int>::type>); + static_assert(!std::same_as<typename Modifier1<S1>::type, + typename Modifier2<S1>::type>); + static_assert(!std::same_as<typename Modifier1<S2>::type, + typename Modifier2<S2>::type>); + static_assert(!std::same_as<typename Modifier1<S3>::type, + typename Modifier2<S3>::type>); + static_assert(!std::same_as<typename Modifier1<S4>::type, + typename Modifier2<S4>::type>); + static_assert(!std::same_as<typename Modifier1<S5>::type, + typename Modifier2<S5>::type>); + static_assert(!std::same_as<typename Modifier1<C1>::type, + typename Modifier2<C1>::type>); + static_assert(!std::same_as<typename Modifier1<C2>::type, + typename Modifier2<C2>::type>); + static_assert(!std::same_as<typename Modifier1<C3>::type, + typename Modifier2<C3>::type>); + static_assert(!std::same_as<typename Modifier1<C4<int> >::type, + typename Modifier2<C4<int> >::type>); + static_assert(!std::same_as<typename Modifier1<C4<int&> >::type, + typename Modifier2<C4<int&> >::type>); + static_assert(!std::same_as<typename Modifier1<C4<int&&> >::type, + typename Modifier2<C4<int&&> >::type>); + static_assert(!std::same_as<typename Modifier1<C5<int> >::type, + typename Modifier2<C5<int> >::type>); + static_assert(!std::same_as<typename Modifier1<C5<int&> >::type, + typename Modifier2<C5<int&> >::type>); + static_assert(!std::same_as<typename Modifier1<C5<int&&> >::type, + typename Modifier2<C5<int&&> >::type>); + static_assert(!std::same_as<typename Modifier1<C6<int> >::type, + typename Modifier2<C6<int> >::type>); + static_assert(!std::same_as<typename Modifier1<C6<int&> >::type, + typename Modifier2<C6<int&> >::type>); + static_assert(!std::same_as<typename Modifier1<C6<int&&> >::type, + typename Modifier2<C6<int&&> >::type>); +} + +template <class T, std::same_as<T> U> +void SubsumptionTest(); + +// clang-format off +template <class T, class U> +requires std::same_as<U, T> && true // NOLINT(readability-simplify-boolean-expr) +int SubsumptionTest(); +// clang-format on + +static_assert(std::same_as<int, decltype(SubsumptionTest<int, int>())>); + +int main() { + { // Checks std::same_as<T, T> is true + CheckSameAs(); + + // Checks std::same_as<T&, T&> is true + CheckSameAs<std::add_lvalue_reference>(); + + // Checks std::same_as<T&&, T&&> is true + CheckSameAs<std::add_rvalue_reference>(); + + // Checks std::same_as<const T, const T> is true + CheckSameAs<std::add_const>(); + } + + { // Checks that `T` and `T&` are distinct types + CheckNotSameAs<identity, std::add_lvalue_reference>(); + CheckNotSameAs<std::add_lvalue_reference, identity>(); + + // Checks that `T` and `T&&` are distinct types + CheckNotSameAs<identity, std::add_rvalue_reference>(); + CheckNotSameAs<std::add_rvalue_reference, identity>(); + + // Checks that `T` and `const T` are distinct types + CheckNotSameAs<identity, std::add_const>(); + CheckNotSameAs<std::add_const, identity>(); + + // Checks `T&` and `T&&` are distinct types + CheckNotSameAs<std::add_lvalue_reference, std::add_rvalue_reference>(); + CheckNotSameAs<std::add_rvalue_reference, std::add_lvalue_reference>(); + } + + { // Checks different type names are distinct types + static_assert(!std::same_as<S1, C1>); + static_assert(!std::same_as<C4<int>, C5<int> >); + static_assert(!std::same_as<C4<int>, C5<int> >); + static_assert(!std::same_as<C5<int, double>, C5<double, int> >); + } +}