Commit 143a1ce1 by Jonathan Wakely Committed by Jonathan Wakely

functional (function::function(F)): LWG 2132: Disable constructor if argument isn't callable.

	* include/std/functional (function::function(F)): LWG 2132: Disable
	constructor if argument isn't callable.
	* testsuite/20_util/function/cons/callable.cc: New.

From-SVN: r186947
parent 112448b4
2012-04-29 Jonathan Wakely <jwakely.gcc@gmail.com>
* include/std/functional (function::function(F)): LWG 2132: Disable
constructor if argument isn't callable.
* testsuite/20_util/function/cons/callable.cc: New.
2012-04-29 Marc Glisse <marc.glisse@inria.fr> 2012-04-29 Marc Glisse <marc.glisse@inria.fr>
PR libstdc++/22200 PR libstdc++/22200
......
...@@ -1856,7 +1856,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -1856,7 +1856,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
{ {
typedef _Function_base::_Base_manager<_Functor*> _Base; typedef _Function_base::_Base_manager<_Functor*> _Base;
public: public:
static bool static bool
_M_manager(_Any_data& __dest, const _Any_data& __source, _M_manager(_Any_data& __dest, const _Any_data& __source,
_Manager_operation __op) _Manager_operation __op)
...@@ -1994,7 +1994,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -1994,7 +1994,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
typedef _Simple_type_wrapper<_Functor> _Wrapper; typedef _Simple_type_wrapper<_Functor> _Wrapper;
typedef _Function_base::_Base_manager<_Wrapper> _Base; typedef _Function_base::_Base_manager<_Wrapper> _Base;
public: public:
static bool static bool
_M_manager(_Any_data& __dest, const _Any_data& __source, _M_manager(_Any_data& __dest, const _Any_data& __source,
_Manager_operation __op) _Manager_operation __op)
...@@ -2038,7 +2038,23 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -2038,7 +2038,23 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
{ {
typedef _Res _Signature_type(_ArgTypes...); typedef _Res _Signature_type(_ArgTypes...);
struct _Useless { }; template<typename _Functor>
using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
(std::declval<_ArgTypes>()...) );
template<typename _CallRes, typename _Res1>
struct _CheckResult
: is_convertible<_CallRes, _Res1> { };
template<typename _CallRes>
struct _CheckResult<_CallRes, void>
: true_type { };
template<typename _Functor>
using _Callable = _CheckResult<_Invoke<_Functor>, _Res>;
template<typename _Cond, typename _Tp>
using _Requires = typename enable_if<_Cond::value, _Tp>::type;
public: public:
typedef _Res result_type; typedef _Res result_type;
...@@ -2099,11 +2115,9 @@ _GLIBCXX_HAS_NESTED_TYPE(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 * If @a __f is a non-NULL function pointer or an object of type @c
* reference_wrapper<F>, this function will not throw. * reference_wrapper<F>, this function will not throw.
*/ */
template<typename _Functor> template<typename _Functor,
function(_Functor __f, typename = _Requires<_Callable<_Functor>, void>>
typename enable_if< function(_Functor);
!is_integral<_Functor>::value, _Useless>::type
= _Useless());
/** /**
* @brief %Function assignment operator. * @brief %Function assignment operator.
...@@ -2178,7 +2192,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -2178,7 +2192,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
* reference_wrapper<F>, this function will not throw. * reference_wrapper<F>, this function will not throw.
*/ */
template<typename _Functor> template<typename _Functor>
typename enable_if<!is_integral<_Functor>::value, function&>::type _Requires<_Callable<_Functor>, function&>
operator=(_Functor&& __f) operator=(_Functor&& __f)
{ {
function(std::forward<_Functor>(__f)).swap(*this); function(std::forward<_Functor>(__f)).swap(*this);
...@@ -2187,7 +2201,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -2187,7 +2201,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
/// @overload /// @overload
template<typename _Functor> template<typename _Functor>
typename enable_if<!is_integral<_Functor>::value, function&>::type function&
operator=(reference_wrapper<_Functor> __f) noexcept operator=(reference_wrapper<_Functor> __f) noexcept
{ {
function(__f).swap(*this); function(__f).swap(*this);
...@@ -2294,11 +2308,9 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) ...@@ -2294,11 +2308,9 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
} }
template<typename _Res, typename... _ArgTypes> template<typename _Res, typename... _ArgTypes>
template<typename _Functor> template<typename _Functor, typename>
function<_Res(_ArgTypes...)>:: function<_Res(_ArgTypes...)>::
function(_Functor __f, function(_Functor __f)
typename enable_if<
!is_integral<_Functor>::value, _Useless>::type)
: _Function_base() : _Function_base()
{ {
typedef _Function_handler<_Signature_type, _Functor> _My_handler; typedef _Function_handler<_Signature_type, _Functor> _My_handler;
......
// { 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
// <http://www.gnu.org/licenses/>.
#include <functional>
#include <testsuite_hooks.h>
void* f(std::function<void()>) { return nullptr; }
int f(std::function<void(int)>) { return 1; }
void test01()
{
void* p __attribute__((unused));
int i __attribute__((unused));
p = f([] { });
i = f([] (int) { });
}
void g(std::function<void()>) { }
void h(std::function<int(int)>) { }
void test02()
{
g([] { return "ignored"; });
h([] (char c) { return c; });
}
int main()
{
test01();
test02();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment