From patchwork Mon Jun 8 20:22:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1305405 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=xd8Be6rT; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49gl6t1cmhz9sRK for ; Tue, 9 Jun 2020 06:22:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D6239388A809; Mon, 8 Jun 2020 20:22:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D6239388A809 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1591647762; bh=AlOpQp+Y95m4B9xYgJReJ0HUI1BaEfHIVHpsAq7SVmk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=xd8Be6rTHRR+xnSZeK7v4ukx23Ql61WyD597dQHYudayuJ9bWqXCp1yl+SE2c+k4W M5zi9wXkgzO1ZzWpHmtq1FV/sX6PHlELVkZ0hmJ+syeLiI0mafgKCOhLQtnrDAiulj vkQtvKMM/1Qw+Q9U5gWZJqsudZMphWwZuWyYmxVg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id CFDEF3877039 for ; Mon, 8 Jun 2020 20:22:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CFDEF3877039 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-411-48MTFY9FNx6obC4Bn2KKxQ-1; Mon, 08 Jun 2020 16:22:35 -0400 X-MC-Unique: 48MTFY9FNx6obC4Bn2KKxQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C335813401; Mon, 8 Jun 2020 20:22:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 618F25C221; Mon, 8 Jun 2020 20:22:12 +0000 (UTC) Date: Mon, 8 Jun 2020 21:22:11 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement operator<< for std::unique_ptr (LWG 2948) Message-ID: <20200608202211.GA425245@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-16.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" libstdc++-v3/ChangeLog: * include/bits/unique_ptr.h (operator<<): Define for C++20. * testsuite/20_util/unique_ptr/io/lwg2948.cc: New test. Tested powerpc64le-linux, committed to master. commit 187da2ce31f13b2f75d5bb780e30ee364ead9d1d Author: Jonathan Wakely Date: Mon Jun 8 18:08:14 2020 +0100 libstdc++: Implement operator<< for std::unique_ptr (LWG 2948) libstdc++-v3/ChangeLog: * include/bits/unique_ptr.h (operator<<): Define for C++20. * testsuite/20_util/unique_ptr/io/lwg2948.cc: New test. diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 3695214808b..d0e4cefadd7 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -39,6 +39,7 @@ #include #if __cplusplus > 201703L # include +# include #endif namespace std _GLIBCXX_VISIBILITY(default) @@ -934,7 +935,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION public __uniq_ptr_hash> { }; -#if __cplusplus > 201103L +#if __cplusplus >= 201402L /// @relates unique_ptr @{ #define __cpp_lib_make_unique 201304 @@ -971,7 +972,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline typename _MakeUniq<_Tp>::__invalid_type make_unique(_Args&&...) = delete; // @} relates unique_ptr -#endif +#endif // C++14 + +#if __cplusplus > 201703L && __cpp_concepts + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2948. unique_ptr does not define operator<< for stream output + /// Stream output operator for unique_ptr + template + inline basic_ostream<_CharT, _Traits>& + operator<<(basic_ostream<_CharT, _Traits>& __os, + const unique_ptr<_Tp, _Dp>& __p) + requires requires { __os << __p.get(); } + { + __os << __p.get(); + return __os; + } +#endif // C++20 // @} group pointer_abstractions diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/io/lwg2948.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/io/lwg2948.cc new file mode 100644 index 00000000000..3f4e4dfabe7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/io/lwg2948.cc @@ -0,0 +1,91 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +template + concept streamable = requires (S& o, const T& p) { o << p; }; + +template + bool + check(const std::unique_ptr& p) + { + std::ostringstream ss1, ss2; + ss1 << p; + ss2 << p.get(); + return ss1.str() == ss2.str(); + } + +void +test01() +{ + static_assert( streamable> ); + + std::unique_ptr p; + VERIFY( check(p) ); + p = std::make_unique(); + VERIFY( check(p) ); +} + +template + struct deleter + { + struct pointer + { + pointer() { } + pointer(std::nullptr_t) { } + explicit operator bool() const { return false; } + bool operator==(pointer) const { return true; } + }; + + void operator()(pointer) const { } + }; + +template + int + operator<<(std::basic_ostream& o, typename deleter::pointer&&) + { + o << C{'P'}; + return 1; // no requirement that this operator returns the stream + } + +template + using Unique_ptr = std::unique_ptr; + +static_assert( streamable>> ); +static_assert( ! streamable>> ); +static_assert( ! streamable>> ); +static_assert( streamable>> ); + +void +test02() +{ + Unique_ptr> p; + VERIFY( check(p) ); +} + +int +main() +{ + test01(); + test02(); +}