xref: /llvm-project/libcxx/test/std/containers/views/mdspan/default_accessor/ctor.conversion.pass.cpp (revision 20c6b9d451ca9af8d23fa7bfa5e18565e761450a)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 // <mdspan>
12 //
13 // Test converting constructor:
14 //
15 // template<class OtherElementType>
16 //   constexpr default_accessor(default_accessor<OtherElementType>) noexcept {}
17 //
18 // Constraints: is_convertible_v<OtherElementType(*)[], element_type(*)[]> is true.
19 
20 #include <mdspan>
21 #include <cassert>
22 #include <cstdint>
23 #include <type_traits>
24 
25 #include "test_macros.h"
26 
27 #include "../MinimalElementType.h"
28 
29 struct Base {};
30 struct Derived: public Base {};
31 
32 template <class FromT, class ToT>
test_conversion()33 constexpr void test_conversion() {
34   std::default_accessor<FromT> acc_from;
35   ASSERT_NOEXCEPT(std::default_accessor<ToT>(acc_from));
36   [[maybe_unused]] std::default_accessor<ToT> acc_to(acc_from);
37 }
38 
test()39 constexpr bool test() {
40   // default accessor conversion largely behaves like pointer conversion
41   test_conversion<int, int>();
42   test_conversion<int, const int>();
43   test_conversion<const int, const int>();
44   test_conversion<MinimalElementType, MinimalElementType>();
45   test_conversion<MinimalElementType, const MinimalElementType>();
46   test_conversion<const MinimalElementType, const MinimalElementType>();
47 
48   // char is convertible to int, but accessors are not
49   static_assert(!std::is_constructible_v<std::default_accessor<int>, std::default_accessor<char>>);
50   // don't allow conversion from const elements to non-const
51   static_assert(!std::is_constructible_v<std::default_accessor<int>, std::default_accessor<const int>>);
52   // MinimalElementType is constructible from int, but accessors should not be convertible
53   static_assert(!std::is_constructible_v<std::default_accessor<MinimalElementType>, std::default_accessor<int>>);
54   // don't allow conversion from const elements to non-const
55   static_assert(!std::is_constructible_v<std::default_accessor<MinimalElementType>, std::default_accessor<const MinimalElementType>>);
56   // don't allow conversion from Base to Derived
57   static_assert(!std::is_constructible_v<std::default_accessor<Derived>, std::default_accessor<Base>>);
58   // don't allow conversion from Derived to Base
59   static_assert(!std::is_constructible_v<std::default_accessor<Base>, std::default_accessor<Derived>>);
60 
61   return true;
62 }
63 
main(int,char **)64 int main(int, char**) {
65   test();
66   static_assert(test());
67   return 0;
68 }
69