|
| 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 | +// cartesian_product_view() = default; |
| 12 | + |
| 13 | +#include <ranges> |
| 14 | + |
| 15 | +#include <cassert> |
| 16 | +#include <type_traits> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +constexpr int buff[] = {1, 2, 3}; |
| 20 | + |
| 21 | +struct DefaultConstructibleView : std::ranges::view_base { |
| 22 | + constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 3) {} |
| 23 | + constexpr int const* begin() const { return begin_; } |
| 24 | + constexpr int const* end() const { return end_; } |
| 25 | + |
| 26 | +private: |
| 27 | + int const* begin_; |
| 28 | + int const* end_; |
| 29 | +}; |
| 30 | + |
| 31 | +struct NoDefaultCtrView : std::ranges::view_base { |
| 32 | + NoDefaultCtrView() = delete; |
| 33 | + int* begin() const; |
| 34 | + int* end() const; |
| 35 | +}; |
| 36 | + |
| 37 | +// The default constructor requires all underlying views to be default constructible. |
| 38 | +// It is implicitly required by the tuple's constructor. If any of the iterators are |
| 39 | +// not default constructible, cartesian product iterator's =default would be implicitly deleted. |
| 40 | +static_assert(std::is_default_constructible_v<std::ranges::cartesian_product_view<DefaultConstructibleView>>); |
| 41 | +static_assert(std::is_default_constructible_v< |
| 42 | + std::ranges::cartesian_product_view<DefaultConstructibleView, DefaultConstructibleView>>); |
| 43 | +static_assert( |
| 44 | + !std::is_default_constructible_v<std::ranges::cartesian_product_view<DefaultConstructibleView, NoDefaultCtrView>>); |
| 45 | +static_assert( |
| 46 | + !std::is_default_constructible_v<std::ranges::cartesian_product_view<NoDefaultCtrView, NoDefaultCtrView>>); |
| 47 | +static_assert(!std::is_default_constructible_v<std::ranges::cartesian_product_view<NoDefaultCtrView>>); |
| 48 | + |
| 49 | +constexpr bool test() { |
| 50 | + { |
| 51 | + using View = std::ranges::cartesian_product_view<DefaultConstructibleView, DefaultConstructibleView>; |
| 52 | + View v = View(); // the default constructor is not explicit |
| 53 | + assert(v.size() == 9); |
| 54 | + auto it = v.begin(); |
| 55 | + using Value = std::tuple<const int&, const int&>; |
| 56 | + assert(*it++ == Value(1, 1)); |
| 57 | + assert(*it++ == Value(1, 2)); |
| 58 | + assert(*it++ == Value(1, 3)); |
| 59 | + assert(*it++ == Value(2, 1)); |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +int main() { |
| 66 | + test(); |
| 67 | + static_assert(test()); |
| 68 | +} |
0 commit comments