From patchwork Sun Apr 29 23:16:33 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 155777 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]) by ozlabs.org (Postfix) with SMTP id 4052CB702C for ; Mon, 30 Apr 2012 09:17:01 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1336346222; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=uipxL+Z kai//uOgr7h/GH+u2boc=; b=ITjQoUXTiAS8z2OxZtrHKcLTLULQOkcAtmxREjt VWJHlXkgFVVERtWRuZBqABDZEZqJRciAmlg4W4f0G+wBoS7MGOJl8zraEdmZZ6GE HtzjN0CgS+OsuSMByWIj1zassb5jNKi2l424EXwGtyKiU2nKNxFGt2Jj9xHlTTRu xen8= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=LTULJPJMyqbVH11XhAp+nOyCJbvkegzJ/lNx1dLZhe0ApXgN8AQndBzjaL3VuA /BWvGuVpwqiLYOV/WCO1Y+4466SgeGQs+PXJvr3sryp3P5Ypuzj4z8vNRe1zgTec dGB/nQKT79mTAyZWrpncUnSGnpoNaZMrtXvHvITHZHYQQ=; Received: (qmail 31366 invoked by alias); 29 Apr 2012 23:16:51 -0000 Received: (qmail 31346 invoked by uid 22791); 29 Apr 2012 23:16:49 -0000 X-SWARE-Spam-Status: No, hits=-4.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Received: from mail-lb0-f175.google.com (HELO mail-lb0-f175.google.com) (209.85.217.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 29 Apr 2012 23:16:35 +0000 Received: by lbbgo4 with SMTP id go4so1701178lbb.20 for ; Sun, 29 Apr 2012 16:16:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.112.24.164 with SMTP id v4mr9153676lbf.52.1335741393885; Sun, 29 Apr 2012 16:16:33 -0700 (PDT) Received: by 10.112.59.230 with HTTP; Sun, 29 Apr 2012 16:16:33 -0700 (PDT) Date: Mon, 30 Apr 2012 00:16:33 +0100 Message-ID: Subject: [v3] constrain std::function constructor to only accept callable types From: Jonathan Wakely To: "libstdc++" , gcc-patches 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 Currently we only constrain std::function's constructor to reject integral arguments, this patch changes it to reject non-callable arguments. This is the proposed resolution of LWG 2132, I had already planned to do this anyway before the issue was opened so I don't see any need to wait for a DR. * include/std/functional (function::function(F)): LWG 2132: Disable constructor if argument isn't callable. * testsuite/20_util/function/cons/callable.cc: New. Tested x86_64-linux, committed to trunk. commit 0e069c4221d6ed4fda7d10938470c472170dcad7 Author: Jonathan Wakely Date: Tue Feb 14 22:22:48 2012 +0000 * include/std/functional (function::function(F)): LWG 2132: Disable constructor if argument isn't callable. * testsuite/20_util/function/cons/callable.cc: New. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 980c6ab..0edb4f1 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -1856,7 +1856,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) { typedef _Function_base::_Base_manager<_Functor*> _Base; - public: + public: static bool _M_manager(_Any_data& __dest, const _Any_data& __source, _Manager_operation __op) @@ -1994,7 +1994,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) typedef _Simple_type_wrapper<_Functor> _Wrapper; typedef _Function_base::_Base_manager<_Wrapper> _Base; - public: + public: static bool _M_manager(_Any_data& __dest, const _Any_data& __source, _Manager_operation __op) @@ -2038,7 +2038,23 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) { typedef _Res _Signature_type(_ArgTypes...); - struct _Useless { }; + template + using _Invoke = decltype(__callable_functor(std::declval<_Functor&>()) + (std::declval<_ArgTypes>()...) ); + + template + struct _CheckResult + : is_convertible<_CallRes, _Res1> { }; + + template + struct _CheckResult<_CallRes, void> + : true_type { }; + + template + using _Callable = _CheckResult<_Invoke<_Functor>, _Res>; + + template + using _Requires = typename enable_if<_Cond::value, _Tp>::type; public: typedef _Res result_type; @@ -2099,11 +2115,9 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) * If @a __f is a non-NULL function pointer or an object of type @c * reference_wrapper, this function will not throw. */ - template - function(_Functor __f, - typename enable_if< - !is_integral<_Functor>::value, _Useless>::type - = _Useless()); + template, void>> + function(_Functor); /** * @brief %Function assignment operator. @@ -2178,7 +2192,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) * reference_wrapper, this function will not throw. */ template - typename enable_if::value, function&>::type + _Requires<_Callable<_Functor>, function&> operator=(_Functor&& __f) { function(std::forward<_Functor>(__f)).swap(*this); @@ -2187,7 +2201,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) /// @overload template - typename enable_if::value, function&>::type + function& operator=(reference_wrapper<_Functor> __f) noexcept { function(__f).swap(*this); @@ -2294,11 +2308,9 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) } template - template + template function<_Res(_ArgTypes...)>:: - function(_Functor __f, - typename enable_if< - !is_integral<_Functor>::value, _Useless>::type) + function(_Functor __f) : _Function_base() { typedef _Function_handler<_Signature_type, _Functor> _My_handler; diff --git a/libstdc++-v3/testsuite/20_util/function/cons/callable.cc b/libstdc++-v3/testsuite/20_util/function/cons/callable.cc new file mode 100644 index 0000000..209c404 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function/cons/callable.cc @@ -0,0 +1,51 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } + +// Copyright (C) 2012 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 +// . + +#include +#include + +void* f(std::function) { return nullptr; } +int f(std::function) { return 1; } + +void test01() +{ + void* p __attribute__((unused)); + int i __attribute__((unused)); + + p = f([] { }); + i = f([] (int) { }); +} + +void g(std::function) { } +void h(std::function) { } + +void test02() +{ + g([] { return "ignored"; }); + h([] (char c) { return c; }); +} + +int main() +{ + test01(); + test02(); + + return 0; +}