[libcxx-commits] [libcxx] 18fe3fe - [libc++] Implements concept constructible_from

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 28 09:32:55 PST 2021


Author: Mark de Wever
Date: 2021-01-28T18:32:47+01:00
New Revision: 18fe3fe0e74b161c723e1e5958ead795ca4fdb7e

URL: https://github.com/llvm/llvm-project/commit/18fe3fe0e74b161c723e1e5958ead795ca4fdb7e
DIFF: https://github.com/llvm/llvm-project/commit/18fe3fe0e74b161c723e1e5958ead795ca4fdb7e.diff

LOG: [libc++] Implements concept constructible_from

Implements parts of:
- P0898R3 Standard Library Concepts
- P1754 Rename concepts to standard_case for C++20, while we still can

Depends on: D91004

Reviewed By: ldionne, cjdb, #libc

Differential Revision: https://reviews.llvm.org/D91986

Added: 
    libcxx/test/std/concepts/concept.constructible/constructible_from.compile.pass.cpp

Modified: 
    libcxx/include/concepts

Removed: 
    


################################################################################
diff  --git a/libcxx/include/concepts b/libcxx/include/concepts
index cf5f9d63971f..86e0325c2b10 100644
--- a/libcxx/include/concepts
+++ b/libcxx/include/concepts
@@ -162,6 +162,11 @@ concept same_as = __same_as_impl<_Tp, _Up> && __same_as_impl<_Up, _Tp>;
 template<class _Tp>
 concept destructible = _VSTD::is_nothrow_destructible_v<_Tp>;
 
+// [concept.constructible]
+template<class _Tp, class... _Args>
+concept constructible_from =
+    destructible<_Tp> && _VSTD::is_constructible_v<_Tp, _Args...>;
+
 #endif //_LIBCPP_STD_VER > 17 && defined(__cpp_concepts) && __cpp_concepts >= 201811L
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/test/std/concepts/concept.constructible/constructible_from.compile.pass.cpp b/libcxx/test/std/concepts/concept.constructible/constructible_from.compile.pass.cpp
new file mode 100644
index 000000000000..2e59b42c3da1
--- /dev/null
+++ b/libcxx/test/std/concepts/concept.constructible/constructible_from.compile.pass.cpp
@@ -0,0 +1,151 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+
+// template<class T, class... Args>
+// concept constructible_from;
+//    destructible<T> && is_constructible_v<T, Args...>;
+
+#include <array>
+#include <concepts>
+#include <memory>
+#include <string>
+#include <type_traits>
+
+struct Empty {};
+
+struct Defaulted {
+  ~Defaulted() = default;
+};
+struct Deleted {
+  ~Deleted() = delete;
+};
+
+struct Noexcept {
+  ~Noexcept() noexcept;
+};
+struct NoexceptTrue {
+  ~NoexceptTrue() noexcept(true);
+};
+struct NoexceptFalse {
+  ~NoexceptFalse() noexcept(false);
+};
+
+struct Protected {
+protected:
+  ~Protected() = default;
+};
+struct Private {
+private:
+  ~Private() = default;
+};
+
+template <class T>
+struct NoexceptDependant {
+  ~NoexceptDependant() noexcept(std::is_same_v<T, int>);
+};
+
+template <class T, class... Args>
+void test() {
+  static_assert(std::constructible_from<T, Args...> ==
+                (std::destructible<T> && std::is_constructible_v<T, Args...>));
+}
+
+void test() {
+  test<bool>();
+  test<bool, bool>();
+
+  test<char>();
+  test<char, char>();
+  test<char, int>();
+
+  test<int>();
+  test<int, int>();
+  test<int, int, int>();
+
+  test<double, int>();
+  test<double, float>();
+  test<double, long double>();
+
+  test<void>();
+  test<void, bool>();
+  test<void, int>();
+
+  test<void*>();
+  test<void*, std::nullptr_t>();
+
+  test<int*>();
+  test<int*, std::nullptr_t>();
+  test<int[], int, int, int>();
+  test<int[1]>();
+  test<int[1], int>();
+  test<int[1], int, int>();
+
+  test<int (*)(int)>();
+  test<int (*)(int), int>();
+  test<int (*)(int), double>();
+  test<int (*)(int), std::nullptr_t>();
+  test<int (*)(int), int (*)(int)>();
+
+  test<void (Empty::*)(const int&)>();
+  test<void (Empty::*)(const int&), std::nullptr_t>();
+  test<void (Empty::*)(const int&) const>();
+  test<void (Empty::*)(const int&) const, void (Empty::*)(const int&)>();
+  test<void (Empty::*)(const int&) volatile>();
+  test<void (Empty::*)(const int&) volatile,
+       void (Empty::*)(const int&) const volatile>();
+  test<void (Empty::*)(const int&) const volatile>();
+  test<void (Empty::*)(const int&) const volatile, double>();
+  test<void (Empty::*)(const int&)&>();
+  test<void (Empty::*)(const int&)&, void (Empty::*)(const int&) &&>();
+  test<void (Empty::*)(const int&) &&>();
+  test<void (Empty::*)(const int&)&&, void (Empty::*)(const int&)>();
+  test<void (Empty::*)(const int&) throw()>();
+  test<void (Empty::*)(const int&) throw(),
+       void(Empty::*)(const int&) noexcept(true)>();
+  test<void (Empty::*)(const int&) noexcept>();
+  test<void (Empty::*)(const int&) noexcept(true)>();
+  test<void (Empty::*)(const int&) noexcept(true),
+       void (Empty::*)(const int&) noexcept(false)>();
+  test<void (Empty::*)(const int&) noexcept(false)>();
+
+  test<int&>();
+  test<int&, int>();
+  test<int&&>();
+  test<int&&, int>();
+
+  test<Empty>();
+
+  test<Defaulted>();
+  test<Deleted>();
+
+  test<NoexceptTrue>();
+  test<NoexceptFalse>();
+  test<Noexcept>();
+
+  test<Protected>();
+  test<Private>();
+
+  test<NoexceptDependant<int> >();
+  test<NoexceptDependant<double> >();
+
+  test<std::string, char*>();
+  test<std::string, const char*>();
+  test<std::string, std::string&>();
+  test<std::string, std::initializer_list<char> >();
+
+  test<std::unique_ptr<int>, std::unique_ptr<int> >();
+  test<std::unique_ptr<int>, std::unique_ptr<int>&>();
+  test<std::unique_ptr<int>, std::unique_ptr<int>&&>();
+
+  test<std::array<int, 1> >();
+  test<std::array<int, 1>, int>();
+  test<std::array<int, 1>, int, int>();
+}


        


More information about the libcxx-commits mailing list