brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · ec104df Raw
104 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// template <class I>12// struct in_found_result;13 14#include <algorithm>15#include <cassert>16#include <type_traits>17 18#include "MoveOnly.h"19 20struct A {21  explicit A(int);22};23// no implicit conversion24static_assert(!std::is_constructible_v<std::ranges::in_found_result<A>, std::ranges::in_found_result<int>>);25 26struct B {27  B(const int&);28  B(int&&);29};30// implicit conversion31static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, std::ranges::in_found_result<int>>);32static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, std::ranges::in_found_result<int>&>);33static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, const std::ranges::in_found_result<int>>);34static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, const std::ranges::in_found_result<int>&>);35 36struct C {37  C(int&);38};39static_assert(!std::is_constructible_v<std::ranges::in_found_result<C>, std::ranges::in_found_result<int>&>);40 41// has to be convertible via const&42static_assert(std::is_convertible_v<std::ranges::in_found_result<int>&, std::ranges::in_found_result<long>>);43static_assert(std::is_convertible_v<const std::ranges::in_found_result<int>&, std::ranges::in_found_result<long>>);44static_assert(std::is_convertible_v<std::ranges::in_found_result<int>&&, std::ranges::in_found_result<long>>);45static_assert(std::is_convertible_v<const std::ranges::in_found_result<int>&&, std::ranges::in_found_result<long>>);46 47// should be move constructible48static_assert(std::is_move_constructible_v<std::ranges::in_found_result<MoveOnly>>);49 50// should not be copy constructible51static_assert(!std::is_copy_constructible_v<std::ranges::in_found_result<MoveOnly>>);52 53struct NotConvertible {};54// conversions should not work if there is no conversion55static_assert(!std::is_convertible_v<std::ranges::in_found_result<NotConvertible>, std::ranges::in_found_result<int>>);56 57static_assert(std::is_same_v<decltype(std::ranges::in_found_result<int>::in), int>);58static_assert(std::is_same_v<decltype(std::ranges::in_found_result<int>::found), bool>);59 60template <class T>61struct ConvertibleFrom {62  constexpr ConvertibleFrom(T c) : content{c} {}63  T content;64};65 66constexpr bool test() {67  // Checks that conversion operations are correct.68  {69    std::ranges::in_found_result<double> res{10, true};70    assert(res.in == 10);71    assert(res.found == true);72    std::ranges::in_found_result<ConvertibleFrom<int>> res2 = res;73    assert(res2.in.content == 10);74    assert(res2.found == true);75  }76 77  // Checks that conversions are possible when one of the types is move-only.78  {79    std::ranges::in_found_result<MoveOnly> res{MoveOnly{}, false};80    assert(res.in.get() == 1);81    assert(!res.found);82    auto res2 = std::move(res);83    assert(res2.in.get() == 1);84    assert(!res2.found);85    assert(res.in.get() == 0);86    assert(!res.found);87  }88 89  // Checks that structured bindings get the correct values.90  {91    auto [in, found] = std::ranges::in_found_result<int>{2, false};92    assert(in == 2);93    assert(!found);94  }95  return true;96}97 98int main(int, char**) {99  test();100  static_assert(test());101 102  return 0;103}104