//===----------------------------------------------------------------------===// // // 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, c++20 // // template struct pair // template // constexpr explicit(see-below) pair(P&&); // since C++23 #include #include #include #include #include #include #include namespace my_ns{ struct MyPairLike { template friend int get(MyPairLike const&) { return 0; } }; } // namespace my_ns template <> struct std::tuple_size : std::integral_constant {}; template struct std::tuple_element { using type = int; }; // https://llvm.org/PR65620 // This used to be a hard error static_assert(!std::is_constructible_v, my_ns::MyPairLike const&>); constexpr bool test() { // Make sure construction works from array, tuple, and ranges::subrange { // Check from std::array { std::array a = {1, 2}; std::pair p(a); assert(p.first == 1); assert(p.second == 2); static_assert(!std::is_constructible_v, std::array>); // too small static_assert( std::is_constructible_v, std::array>); // works (test the test) static_assert(!std::is_constructible_v, std::array>); // too large } // Check from std::tuple { std::tuple a = {1, 2}; std::pair p(a); assert(p.first == 1); assert(p.second == 2); static_assert(!std::is_constructible_v, std::tuple>); // too small static_assert( std::is_constructible_v, std::tuple>); // works (test the test) static_assert(!std::is_constructible_v, std::tuple>); // too large } // Check that the constructor excludes ranges::subrange { int data[] = {1, 2, 3, 4, 5}; const std::ranges::subrange a(data); // Note the expression below would be ambiguous if pair's // constructor does not exclude subrange std::pair p = a; assert(p.first == data + 0); assert(p.second == data + 5); } } // Make sure we allow element conversion from a pair-like { std::tuple a = {34, "hello world"}; std::pair p(a); assert(p.first == 34); assert(p.second == std::string("hello world")); static_assert(!std::is_constructible_v, std::tuple>); // first not convertible static_assert(!std::is_constructible_v, std::tuple>); // second not convertible static_assert( std::is_constructible_v, std::tuple>); // works (test the test) } // Make sure we forward the pair-like elements { struct NoCopy { NoCopy() = default; NoCopy(NoCopy const&) = delete; NoCopy(NoCopy&&) = default; }; std::tuple a; std::pair p(std::move(a)); (void)p; } // Make sure the constructor is implicit iff both elements can be converted { struct To { }; struct FromImplicit { constexpr operator To() const { return To{}; } }; struct FromExplicit { constexpr explicit operator To() const { return To{}; } }; // If both are convertible, the constructor is not explicit { std::tuple a = {FromImplicit{}, 2.3f}; std::pair p = a; (void)p; static_assert(std::is_convertible_v, std::pair>); } // Otherwise, the constructor is explicit { static_assert( std::is_constructible_v, std::tuple>); static_assert(!std::is_convertible_v, std::pair>); static_assert( std::is_constructible_v, std::tuple>); static_assert(!std::is_convertible_v, std::pair>); static_assert( std::is_constructible_v, std::tuple>); static_assert(!std::is_convertible_v, std::pair>); } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }