From patchwork Fri Nov 1 11:12:59 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 287790 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0401C2C0086 for ; Fri, 1 Nov 2013 22:13:16 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; q= dns; s=default; b=lONg9gncv5hDo9xmj0sNtLDtBGJ6zN/wpttVOGo3d8zeXv I8mhiqjUJ92XQqamA0Hno6NzHkZfGYy/faqX/bry1WMlYtI0s0GN/J5Li2kAmzXa 4l09eSzUCc74Osr4cFyrSVflhpVUStTvp8DFIE9xuvsj70QR04Plhw2UbxGls= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:date:message-id:subject:from:to:content-type; s= default; bh=Dc/txCi6/D9kQ/OTOI4/13CJZWM=; b=MFwsfzmNQJzGLVPPl4qA JHtpV/ckSGOCId2q6uUtEbDJeaoQesX99CskLJ6b/TyIeJ7pB4VJ5/bEPaT4vkpK ntB0v4zEPCClNMXManMx9kUIvzsS3YmGicyRRP/yV5njXda83ikYQ/kzgmmB4ECL 6wi62aA4dodNHAnkHnN5JzY= Received: (qmail 10258 invoked by alias); 1 Nov 2013 11:13:07 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 10234 invoked by uid 89); 1 Nov 2013 11:13:06 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-lb0-f181.google.com Received: from mail-lb0-f181.google.com (HELO mail-lb0-f181.google.com) (209.85.217.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 01 Nov 2013 11:13:04 +0000 Received: by mail-lb0-f181.google.com with SMTP id x18so3294102lbi.40 for ; Fri, 01 Nov 2013 04:13:00 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.168.105 with SMTP id zv9mr17292lbb.48.1383304379971; Fri, 01 Nov 2013 04:12:59 -0700 (PDT) Received: by 10.112.101.7 with HTTP; Fri, 1 Nov 2013 04:12:59 -0700 (PDT) Date: Fri, 1 Nov 2013 11:12:59 +0000 Message-ID: Subject: [v3 patch] Implement N3421 - make functors greater<> From: Jonathan Wakely To: "libstdc++" , gcc-patches This implements another piece of the C++14 library, STL's "transparent functors" proposal. I also added the is_transparent typedefs, which come from another proposal but modify these "diamond operators". I hope to get round to using those typedefs to implement Joaquin's heterogeneous lookup soon. 2013-11-01 Jonathan Wakely N3421 C++1y Transparent functors * include/bits/stl_function.h (plus, minus, multiplies, divides, modulus, negate, equal_to, not_equal_to, greater, less, greater_equal, less_equal, logical_and, logical_or, logical_not, bit_and, bit_or, bit_xor, bit_not): Define. * doc/xml/manual/status_cxx2014.xml: Update. * testsuite/20_util/function_objects/comparisons_void.cc: New. Tested x86_64-linux, committed to trunk. commit 8382a58b1ba77be9f575caad84506362b2c87ad1 Author: Jonathan Wakely Date: Tue May 21 08:55:24 2013 +0100 N3421 C++1y Transparent functors * include/bits/stl_function.h (plus, minus, multiplies, divides, modulus, negate, equal_to, not_equal_to, greater, less, greater_equal, less_equal, logical_and, logical_or, logical_not, bit_and, bit_or, bit_xor, bit_not): Define. * doc/xml/manual/status_cxx2014.xml: Update. * testsuite/20_util/function_objects/comparisons_void.cc: New. * include/bits/stl_function.h: Implement N3421. * testsuite/20_util/function_objects/comparisons_void.cc: New. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2014.xml b/libstdc++-v3/doc/xml/manual/status_cxx2014.xml index 4ef4334..b368a81 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2014.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2014.xml @@ -209,7 +209,7 @@ particular release. Making Operator Functors greater<> - WIP + Y diff --git a/libstdc++-v3/include/bits/stl_function.h b/libstdc++-v3/include/bits/stl_function.h index 23529df..d2e5d13 100644 --- a/libstdc++-v3/include/bits/stl_function.h +++ b/libstdc++-v3/include/bits/stl_function.h @@ -56,6 +56,10 @@ #ifndef _STL_FUNCTION_H #define _STL_FUNCTION_H 1 +#if __cplusplus > 201103L +#include +#endif + namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -135,6 +139,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * * @{ */ + +#if __cplusplus > 201103L + struct __is_transparent; // undefined + + template + struct plus; + + template + struct minus; + + template + struct multiplies; + + template + struct divides; + + template + struct modulus; + + template + struct negate; +#endif + /// One of the @link arithmetic_functors math functors@endlink. template struct plus : public binary_function<_Tp, _Tp, _Tp> @@ -188,6 +215,91 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(const _Tp& __x) const { return -__x; } }; + +#if __cplusplus > 201103L + template<> + struct plus + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) + std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) + std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) + std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link arithmetic_functors math functors@endlink. + template<> + struct minus + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) - std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) - std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) - std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link arithmetic_functors math functors@endlink. + template<> + struct multiplies + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) * std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) * std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) * std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link arithmetic_functors math functors@endlink. + template<> + struct divides + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) / std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) / std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) / std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link arithmetic_functors math functors@endlink. + template<> + struct modulus + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) % std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) % std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) % std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link arithmetic_functors math functors@endlink. + template<> + struct negate + { + template + auto + operator()(_Tp&& __t) const + noexcept(noexcept(-std::forward<_Tp>(__t))) + -> decltype(-std::forward<_Tp>(__t)) + { return -std::forward<_Tp>(__t); } + + typedef __is_transparent is_transparent; + }; +#endif /** @} */ // 20.3.3 comparisons @@ -199,6 +311,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * * @{ */ +#if __cplusplus > 201103L + template + struct equal_to; + + template + struct not_equal_to; + + template + struct greater; + + template + struct less; + + template + struct greater_equal; + + template + struct less_equal; +#endif + /// One of the @link comparison_functors comparison functors@endlink. template struct equal_to : public binary_function<_Tp, _Tp, bool> @@ -252,6 +384,92 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; } }; + +#if __cplusplus > 201103L + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct equal_to + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) == std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) == std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) == std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct not_equal_to + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) != std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) != std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) != std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct greater + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) > std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) > std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) > std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct less + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct greater_equal + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) >= std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) >= std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) >= std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link comparison_functors comparison functors@endlink. + template<> + struct less_equal + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) <= std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) <= std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) <= std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; +#endif /** @} */ // 20.3.4 logical operations @@ -263,6 +481,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * * @{ */ +#if __cplusplus > 201103L + template + struct logical_and; + + template + struct logical_or; + + template + struct logical_not; +#endif + /// One of the @link logical_functors Boolean operations functors@endlink. template struct logical_and : public binary_function<_Tp, _Tp, bool> @@ -289,8 +518,64 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator()(const _Tp& __x) const { return !__x; } }; + +#if __cplusplus > 201103L + /// One of the @link logical_functors Boolean operations functors@endlink. + template<> + struct logical_and + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) && std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) && std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) && std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link logical_functors Boolean operations functors@endlink. + template<> + struct logical_or + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) || std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) || std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) || std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + /// One of the @link logical_functors Boolean operations functors@endlink. + template<> + struct logical_not + { + template + auto + operator()(_Tp&& __t) const -> decltype(!std::forward<_Tp>(__t)) + { return !std::forward<_Tp>(__t); } + + typedef __is_transparent is_transparent; + }; +#endif /** @} */ +#if __cplusplus > 201103L + template + struct bit_and; + + template + struct bit_or; + + template + struct bit_xor; + + template + struct bit_not; +#endif + // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 660. Missing Bitwise Operations. template @@ -317,6 +602,68 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return __x ^ __y; } }; + template + struct bit_not : public unary_function<_Tp, _Tp> + { + _Tp + operator()(const _Tp& __x) const + { return ~__x; } + }; + +#if __cplusplus > 201103L + template <> + struct bit_and + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) & std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) & std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) & std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + template <> + struct bit_or + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) | std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) | std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) | std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + template <> + struct bit_xor + { + template + auto + operator()(_Tp&& __t, _Up&& __u) const + noexcept(noexcept(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u))) + -> decltype(std::forward<_Tp>(__t) ^ std::forward<_Up>(__u)) + { return std::forward<_Tp>(__t) ^ std::forward<_Up>(__u); } + + typedef __is_transparent is_transparent; + }; + + template <> + struct bit_not + { + template + auto + operator()(_Tp&& __t) const + noexcept(noexcept(~std::forward<_Tp>(__t))) + -> decltype(~std::forward<_Tp>(__t)) + { return ~std::forward<_Tp>(__t); } + + typedef __is_transparent is_transparent; + }; +#endif + // 20.3.5 negators /** @defgroup negators Negators * @ingroup functors diff --git a/libstdc++-v3/testsuite/20_util/function_objects/comparisons_void.cc b/libstdc++-v3/testsuite/20_util/function_objects/comparisons_void.cc new file mode 100644 index 0000000..2cea1fc --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/comparisons_void.cc @@ -0,0 +1,95 @@ +// { dg-options " -std=gnu++1y " } +// { dg-do compile } + +// Copyright (C) 2013 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 +// . + +// 20.3.3 Comparisons + +#include + +struct R { }; + +struct L +{ + L operator+(const R&) const { return *this; } + L operator-(const R&) const { return *this; } + L operator*(const R&) const { return *this; } + L operator/(const R&) const { return *this; } + L operator%(const R&) const { return *this; } + L operator-() const { return *this; } + + bool operator==(const R&) const { return true; } + bool operator!=(const R&) const { return false; } + bool operator<(const R&) const { return false; } + bool operator<=(const R&) const { return true; } + bool operator>(const R&) const { return false; } + bool operator>=(const R&) const { return true; } + + bool operator&&(const R&) const { return true; } + bool operator||(const R&) const { return true; } + bool operator!() const { return false; } + + int operator&(const R&) const { return 1; } + int operator|(const R&) const { return 1; } + int operator^(const R&) const { return 0; } + int operator~() const { return 0; } +}; + +L l; +R r; + +// test unary function objects +template +bool +test1(F f) +{ + f(l); + return true; +} + +// test binary function objects +template +bool +test2(F f) +{ + f(l, r); + return true; +} + +auto plus = test2( std::plus<>() ); +auto minus = test2( std::minus<>() ); +auto multiplies = test2( std::multiplies<>() ); +auto divides = test2( std::divides<>() ); +auto modulus = test2( std::modulus<>() ); +auto negate = test1( std::negate<>() ); + +auto equal_to = test2( std::equal_to<>() ); +auto not_equal_to = test2( std::not_equal_to<>() ); +auto greater = test2( std::greater<>() ); +auto less = test2( std::less<>() ); +auto greater_equal = test2( std::greater_equal<>() ); +auto less_equal = test2( std::less_equal<>() ); + +auto logical_and = test2( std::logical_and<>() ); +auto logical_or = test2( std::logical_or<>() ); +auto logical_not = test1( std::logical_not<>() ); + +auto bit_and = test2( std::bit_and<>() ); +auto bit_or = test2( std::bit_or<>() ); +auto bit_xor = test2( std::bit_xor<>() ); +auto bit_not = test1( std::bit_not<>() );