xref: /llvm-project/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp (revision d2baefae6846765eef6a6dd69d4fdf1082ce29ad)
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
10 
11 // template<class I2>
12 //   requires convertible_to<const I2&, I>
13 //     constexpr counted_iterator(const counted_iterator<I2>& x);
14 
15 #include <iterator>
16 
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 
20 template<class T>
21 class ConvertibleTo
22 {
23     int *it_;
24 
25 public:
26     typedef          std::input_iterator_tag                      iterator_category;
27     typedef int                                                   value_type;
28     typedef typename std::iterator_traits<int *>::difference_type difference_type;
29     typedef int *                                                 pointer;
30     typedef int &                                                 reference;
31 
base() const32     constexpr int *base() const {return it_;}
33 
34     ConvertibleTo() = default;
ConvertibleTo(int * it)35     explicit constexpr ConvertibleTo(int *it) : it_(it) {}
36 
operator *() const37     constexpr reference operator*() const {return *it_;}
38 
operator ++()39     constexpr ConvertibleTo& operator++() {++it_; return *this;}
operator ++(int)40     constexpr ConvertibleTo operator++(int)
41         {ConvertibleTo tmp(*this); ++(*this); return tmp;}
42 
operator T() const43     constexpr operator T() const { return T(it_); }
44 };
45 
test()46 constexpr bool test() {
47   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
48 
49   {
50     static_assert( std::is_constructible_v<std::counted_iterator<forward_iterator<int*>>,
51                                            std::counted_iterator<forward_iterator<int*>>>);
52     static_assert(!std::is_constructible_v<std::counted_iterator<forward_iterator<int*>>,
53                                            std::counted_iterator<random_access_iterator<int*>>>);
54   }
55   {
56     std::counted_iterator iter1(ConvertibleTo<forward_iterator<int*>>{buffer}, 8);
57     std::counted_iterator<forward_iterator<int*>> iter2(iter1);
58     assert(iter2.base() == forward_iterator<int*>{buffer});
59     assert(iter2.count() == 8);
60   }
61   {
62     const std::counted_iterator iter1(ConvertibleTo<forward_iterator<int*>>{buffer}, 8);
63     const std::counted_iterator<forward_iterator<int*>> iter2(iter1);
64     assert(iter2.base() == forward_iterator<int*>{buffer});
65     assert(iter2.count() == 8);
66   }
67 
68   return true;
69 }
70 
main(int,char **)71 int main(int, char**) {
72   test();
73   static_assert(test());
74 
75   return 0;
76 }
77