RFC defining std::function::assign

Jonathan Wakely jwakely.gcc@gmail.com
Sat Jun 5 19:01:00 GMT 2010


void function::assign(F&&, const Alloc& a);

Currently this function is not defined because it would need to use
the function(allocator_arg_t, const Alloc&, F) constructor, which
doesn't exist either, because we don't have allocator_arg_t yet.

The patch below would define function::assign, but ignore the
allocator argument. That allows the attached test to compile, but it's
XFAILed because the allocator isn't used.

I think it's better to have the assign function, even if it doesn't
use the allocator, but would like to hear what others think.
-------------- next part --------------
Index: include/std/functional
===================================================================
--- include/std/functional	(revision 160324)
+++ include/std/functional	(working copy)
@@ -1952,16 +1952,14 @@ namespace std
 	__x._M_invoker = __old_invoker;
       }
 
-      // TODO: needs allocator_arg_t
-      /*
       template<typename _Functor, typename _Alloc>
         void
         assign(_Functor&& __f, const _Alloc& __a)
         {
-          function(allocator_arg, __a,
+          // TODO: needs allocator_arg_t
+          function( // allocator_arg, __a,
                    std::forward<_Functor>(__f)).swap(*this);
         }
-      */
       
       // [3.7.2.3] function capacity
 
Index: testsuite/20_util/function/assign/allocator.cc
===================================================================
--- testsuite/20_util/function/assign/allocator.cc	(revision 0)
+++ testsuite/20_util/function/assign/allocator.cc	(revision 0)
@@ -0,0 +1,58 @@
+// { dg-options "-std=gnu++0x" }
+// { dg-do run { xfail *-*-* } }
+
+// Copyright (C) 2010 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>
+#include <testsuite_allocator.h>
+
+int f1() { return 1; }
+struct { int operator()() { return 2; } } f2; 
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  typedef std::function<int()> function;
+  typedef __gnu_test::tracker_allocator<char> allocator;
+  typedef __gnu_test::tracker_allocator_counter counter;
+
+  function fo;
+  allocator a;
+
+  fo.assign(f1, a);
+  VERIFY( counter::get_construct_count() == 1 );
+  VERIFY( counter::get_allocation_count() == 0 );
+
+  fo.assign(f2, a);
+  VERIFY( counter::get_destruct_count() == 1 );
+  VERIFY( counter::get_construct_count() == 2 );
+  VERIFY( counter::get_allocation_count() >= sizeof(f2) );
+
+  fo = nullptr;
+  VERIFY( counter::get_destruct_count() == 2 );
+  VERIFY( counter::get_allocation_count()
+          == counter::get_deallocation_count() );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}


More information about the Libstdc++ mailing list