From patchwork Fri Sep 16 16:18:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1257 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp829796wrt; Fri, 16 Sep 2022 09:22:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM60fayMdJkWu6wKIwiWdsQPW/IANvHGVbXaqL/IDNe8RQs1ZLsliIsRDJKYWXiKPhhJRAPm X-Received: by 2002:a05:6402:298b:b0:44f:20a:2db2 with SMTP id eq11-20020a056402298b00b0044f020a2db2mr4657308edb.138.1663345320008; Fri, 16 Sep 2022 09:22:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663345320; cv=none; d=google.com; s=arc-20160816; b=Yhemlv7k0mSf67KIKjk28FtgNLPwYJrh4BAl2TquQTVb9MMMJ75ihx/or0ZetrV1F3 4oGIYh6Eyg1Y0bVJYHxhnsCV3qGjaolKqofk4IT+FcLxgoPHr8v0hp4m3zzAk2Xs6qVG 41XIdLIO3MDykxq4X5Aj6PIUBY70AnMDNHIygGB5ftX792DZaiJQN2pLQy3nEj1I/qQG zftPG3dd2dfFypb0v3U/CdlDhU8VQp2GFAJh3Y/MJbvGebSzmggpjryqOmRwSBb94vNV ArGxkoBRGK/w866I8lAhgg6+1O5fMsoqZb55T2NcsIuc4sWV7HamDaJZencxZs/29CAj /kTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=9LPTu+3uUOtBrHt8dA0C0cT0f4Fd+hZMRHGPejp0rRU=; b=qgZhCJF6M59ZmcInlHIEY8cu9s2owKVBRnNAp1FhA7zX/xceDRO9ZVFiajtuGOp2zC krjbU9vpYbXRJrgoySF7rzQSp1AwyNNeYSwDuuUZZ1hf8QQZJctIoR/3G5ddyRvfWE1J iODKcc7f+TNHtO/VksZr/9KqyKz2xx8JpX8XTXNuw3zizC9MyC6QucJy8FgXvx7p9G9r E27Fi5dSbdIFQR3CY6r2P//FRzZ9ScJzY7PE/85lc3eSwO+UyMVcmihtXmKIA12/G0Xy wzfSVuXaRfoEj6hJUGk75giQM1xlVWFZAZQ2j1WsvdCLCgT/6S04FJrWszPy82W9XXOQ 2rbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="VMCNMns/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m11-20020a50998b000000b004518397040fsi2623831edb.352.2022.09.16.09.21.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Sep 2022 09:21:59 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="VMCNMns/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7E653948A79 for ; Fri, 16 Sep 2022 16:20:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7E653948A79 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663345225; bh=9LPTu+3uUOtBrHt8dA0C0cT0f4Fd+hZMRHGPejp0rRU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VMCNMns/iiZq9xq2O0L+Iis+9X6ZNDEkImqQuIC+oM5XbgkdoQsgFeL1J8Nt/ME02 8bkmmwTj6rL1Yhbo5BNyfvtwCqmRloOeXobGFuVKMDEETVS9U8Sh/zSdbYm5NNhZ7v CdRIp2G3i2g3RY9yhq/YycHZ//quZd/56bXnDGdE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id DE6B2392AC29 for ; Fri, 16 Sep 2022 16:19:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DE6B2392AC29 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-422-34ofrhLUPMq70a3hRgsZFQ-1; Fri, 16 Sep 2022 12:18:59 -0400 X-MC-Unique: 34ofrhLUPMq70a3hRgsZFQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B134A101A528; Fri, 16 Sep 2022 16:18:58 +0000 (UTC) Received: from localhost (unknown [10.33.36.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77BC817593; Fri, 16 Sep 2022 16:18:58 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix tr1::variate_generator::engine_value_type Date: Fri, 16 Sep 2022 17:18:57 +0100 Message-Id: <20220916161857.510663-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744143982459521538?= X-GMAIL-MSGID: =?utf-8?q?1744143982459521538?= Tested x86_64-linux, pushed to trunk. -- >8 -- The tr1/5_numerical_facilities/random/variate_generator/37986.cc test fails for strict -std=c++98 mode because _Adaptor(const _Engine&) is ill-formed in C++98 when _Engine is a reference type. Rather than attempt to make the _Adaptor handle references and pointers, just strip references and pointers from the _Engine type before we adapt it. That removes the need for the _Adaptor<_Engine*> partial specialization and avoids the reference-to-reference problem for c++98 mode. While looking into this I noticed that the TR1 spec requires the variate_generator::engine_value_type to be the underlying engine type, whereas we make it the _Adaptor type that wraps the engine. libstdc++-v3/ChangeLog: * include/tr1/random.h (__detail::_Adaptor::_BEngine): Remove. (__detail::_Adaptor::_M_g): Make public. (__detail::_Adaptor<_Engine*, _Dist>): Remove partial specialization. (variate_generate::_Value): New helper to simplify handling of _Engine* and _Engine& template arguments. (variate_generate::engine_value_type): Define to underlying engine type, not adapted type. (variate_generate::engine()): Return underlying engine instead of adaptor. * testsuite/tr1/5_numerical_facilities/random/variate_generator/37986.cc: Fix comment. * testsuite/tr1/5_numerical_facilities/random/variate_generator/requirements/typedefs.cc: Check member typedefs have the correct types. --- libstdc++-v3/include/tr1/random.h | 115 ++++++------------ .../random/variate_generator/37986.cc | 2 +- .../requirements/typedefs.cc | 51 ++++++-- 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/libstdc++-v3/include/tr1/random.h b/libstdc++-v3/include/tr1/random.h index 535f142a004..6061649c5a4 100644 --- a/libstdc++-v3/include/tr1/random.h +++ b/libstdc++-v3/include/tr1/random.h @@ -81,9 +81,8 @@ namespace tr1 template struct _Adaptor { - typedef typename remove_reference<_Engine>::type _BEngine; - typedef typename _BEngine::result_type _Engine_result_type; - typedef typename _Distribution::input_type result_type; + typedef typename _Engine::result_type _Engine_result_type; + typedef typename _Distribution::input_type result_type; public: _Adaptor(const _Engine& __g) @@ -146,72 +145,8 @@ namespace tr1 return __return_value; } - private: _Engine _M_g; }; - - // Specialization for _Engine*. - template - struct _Adaptor<_Engine*, _Distribution> - { - typedef typename _Engine::result_type _Engine_result_type; - typedef typename _Distribution::input_type result_type; - - public: - _Adaptor(_Engine* __g) - : _M_g(__g) { } - - result_type - min() const - { - result_type __return_value; - if (is_integral<_Engine_result_type>::value - && is_integral::value) - __return_value = _M_g->min(); - else - __return_value = result_type(0); - return __return_value; - } - - result_type - max() const - { - result_type __return_value; - if (is_integral<_Engine_result_type>::value - && is_integral::value) - __return_value = _M_g->max(); - else if (!is_integral::value) - __return_value = result_type(1); - else - __return_value = std::numeric_limits::max() - 1; - return __return_value; - } - - result_type - operator()() - { - result_type __return_value; - if (is_integral<_Engine_result_type>::value - && is_integral::value) - __return_value = (*_M_g)(); - else if (!is_integral<_Engine_result_type>::value - && !is_integral::value) - __return_value = result_type((*_M_g)() - _M_g->min()) - / result_type(_M_g->max() - _M_g->min()); - else if (is_integral<_Engine_result_type>::value - && !is_integral::value) - __return_value = result_type((*_M_g)() - _M_g->min()) - / result_type(_M_g->max() - _M_g->min() + result_type(1)); - else - __return_value = ((((*_M_g)() - _M_g->min()) - / (_M_g->max() - _M_g->min())) - * std::numeric_limits::max()); - return __return_value; - } - - private: - _Engine* _M_g; - }; } // namespace __detail /** @@ -223,17 +158,45 @@ namespace tr1 template class variate_generator { - // Concept requirements. - __glibcxx_class_requires(_Engine, _CopyConstructibleConcept) - // __glibcxx_class_requires(_Engine, _EngineConcept) - // __glibcxx_class_requires(_Dist, _EngineConcept) + template + struct _Value + { + typedef _Eng type; + + static const _Eng& + _S_ref(const _Eng& __e) { return __e; } + }; + + template + struct _Value<_Eng*> + { + typedef _Eng type; + + __attribute__((__nonnull__)) + static const _Eng& + _S_ref(const _Eng* __e) { return *__e; } + }; + + template + struct _Value<_Eng&> + { + typedef _Eng type; + + static const _Eng& + _S_ref(const _Eng& __e) { return __e; } + }; public: typedef _Engine engine_type; - typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type; + typedef typename _Value<_Engine>::type engine_value_type; typedef _Dist distribution_type; typedef typename _Dist::result_type result_type; + // Concept requirements. + __glibcxx_class_requires(engine_value_type, _CopyConstructibleConcept) + // __glibcxx_class_requires(_Engine, _EngineConcept) + // __glibcxx_class_requires(_Dist, _EngineConcept) + // tr1:5.1.1 table 5.1 requirement typedef typename __gnu_cxx::__enable_if< is_arithmetic::value, result_type>::__type _IsValidType; @@ -246,7 +209,7 @@ namespace tr1 * the @p _Engine or @p _Dist objects. */ variate_generator(engine_type __eng, distribution_type __dist) - : _M_engine(__eng), _M_dist(__dist) { } + : _M_engine(_Value<_Engine>::_S_ref(__eng)), _M_dist(__dist) { } /** * Gets the next generated value on the distribution. @@ -269,7 +232,7 @@ namespace tr1 */ engine_value_type& engine() - { return _M_engine; } + { return _M_engine._M_g; } /** * Gets a const reference to the underlying uniform random number @@ -277,7 +240,7 @@ namespace tr1 */ const engine_value_type& engine() const - { return _M_engine; } + { return _M_engine._M_g; } /** * Gets a reference to the underlying random distribution. @@ -308,7 +271,7 @@ namespace tr1 { return this->distribution().max(); } private: - engine_value_type _M_engine; + __detail::_Adaptor _M_engine; distribution_type _M_dist; }; diff --git a/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/37986.cc b/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/37986.cc index 5eeccf04771..a13094740e7 100644 --- a/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/37986.cc +++ b/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/37986.cc @@ -21,7 +21,7 @@ #include -// libtsdc++/37986 +// libstdc++/37986 void test01() { std::tr1::mt19937 mt; diff --git a/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/requirements/typedefs.cc b/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/requirements/typedefs.cc index 0bdb610e31b..a71c8ddfd81 100644 --- a/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/requirements/typedefs.cc +++ b/libstdc++-v3/testsuite/tr1/5_numerical_facilities/random/variate_generator/requirements/typedefs.cc @@ -23,19 +23,56 @@ #include +template struct require_same; // not defined +template struct require_same { }; + +typedef std::tr1::linear_congruential E; +typedef std::tr1::uniform_int D; + void test01() { - using namespace std::tr1; - - typedef variate_generator - < - linear_congruential, - uniform_int - > test_type; + typedef std::tr1::variate_generator test_type; typedef test_type::engine_type engine_type; typedef test_type::engine_value_type engine_value_type; typedef test_type::distribution_type distribution_type; typedef test_type::result_type result_type; + + require_same check_e; + require_same check_ev; + require_same check_d; + require_same check_r; +} + +void +test02() +{ + typedef std::tr1::variate_generator test_type; + + typedef test_type::engine_type engine_type; + typedef test_type::engine_value_type engine_value_type; + typedef test_type::distribution_type distribution_type; + typedef test_type::result_type result_type; + + require_same check_e; + require_same check_ev; + require_same check_d; + require_same check_r; +} + +void +test03() +{ + typedef std::tr1::variate_generator test_type; + + typedef test_type::engine_type engine_type; + typedef test_type::engine_value_type engine_value_type; + typedef test_type::distribution_type distribution_type; + typedef test_type::result_type result_type; + + require_same check_e; + require_same check_ev; + require_same check_d; + require_same check_r; }