diff mbox

[v3] Add is_nothrow_constructible

Message ID 4C1034FF.3050806@oracle.com
State New
Headers show

Commit Message

Paolo Carlini June 10, 2010, 12:42 a.m. UTC
Hi,

tested x86_64-linux, committed to mainline.

Paolo.

//////////////////
2010-06-09  Paolo Carlini  <paolo.carlini@oracle.com>

	* include/std/type_traits (is_nothrow_constructible): Add.
	(declval): Add noexcept specification.
	* testsuite/util/testsuite_tr1.h (struct NothrowExplicitClass,
	ThrowExplicitClass, NoexceptExplicitClass, ExceptExplicitClass):
	Add.
	* testsuite/20_util/is_nothrow_constructible/value.cc: New.
	* testsuite/20_util/is_nothrow_constructible/requirements/
	typedefs.cc: Likewise.
	* testsuite/20_util/is_nothrow_constructible/requirements/
	explicit_instantiation.cc: Likewise.
	* testsuite/20_util/make_signed/requirements/typedefs_neg.cc:
	Adjust dg-error line numbers.
	* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
	Likewise.
	* testsuite/20_util/declval/requirements/1_neg.cc: Likewise.
diff mbox

Patch

Index: include/std/type_traits
===================================================================
--- include/std/type_traits	(revision 160490)
+++ include/std/type_traits	(working copy)
@@ -193,7 +193,7 @@ 
     { };
 
   template<typename _Tp>
-    typename add_rvalue_reference<_Tp>::type declval();
+    typename add_rvalue_reference<_Tp>::type declval() noexcept;
 
   template<typename _Tp, typename... _Args>
     class __is_constructible_helper
@@ -234,6 +234,28 @@ 
 							 _Args...>::__value>
     { };
 
+  template<bool, typename _Tp, typename... _Args>
+    struct __is_nt_constructible_helper
+    { static const bool __value = false; };
+
+  template<typename _Tp, typename... _Args>
+    struct __is_nt_constructible_helper<true, _Tp, _Args...>
+    { static const bool __value = noexcept(_Tp(declval<_Args>()...)); };
+
+  template<typename _Tp, typename _Arg>
+    struct __is_nt_constructible_helper<true, _Tp, _Arg>
+    {
+      static const bool __value = noexcept(static_cast<_Tp>(declval<_Arg>()));
+    };
+
+  /// is_nothrow_constructible
+  template<typename _Tp, typename... _Args>
+    struct is_nothrow_constructible
+    : public integral_constant<bool,
+	  __is_nt_constructible_helper<is_constructible<_Tp, _Args...>::value,
+				       _Tp, _Args...>::__value>
+    { };
+
   /// has_trivial_default_constructor
   template<typename _Tp>
     struct has_trivial_default_constructor
@@ -649,7 +671,7 @@ 
 
   template<typename _Tp>
     inline typename add_rvalue_reference<_Tp>::type
-    declval()
+    declval() noexcept
     {
       static_assert(__declval_protector<_Tp>::__stop,
 		    "declval() must not be used!");
Index: testsuite/util/testsuite_tr1.h
===================================================================
--- testsuite/util/testsuite_tr1.h	(revision 160490)
+++ testsuite/util/testsuite_tr1.h	(working copy)
@@ -1,7 +1,8 @@ 
 // -*- C++ -*-
 // Testing utilities for the tr1 testsuite.
 //
-// Copyright (C) 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
+// Copyright (C) 2004, 2005, 2006, 2007, 2009, 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
@@ -127,8 +128,39 @@ 
   {
     ExplicitClass(double&);
     explicit ExplicitClass(int&);
+    ExplicitClass(double&, int&, double&);
   };
 
+  struct NothrowExplicitClass
+  {
+    NothrowExplicitClass(double&) throw();
+    explicit NothrowExplicitClass(int&) throw();
+    NothrowExplicitClass(double&, int&, double&) throw();
+  };
+
+  struct ThrowExplicitClass
+  {
+    ThrowExplicitClass(double&) throw(int);
+    explicit ThrowExplicitClass(int&) throw(int);
+    ThrowExplicitClass(double&, int&, double&) throw(int);
+  };
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+  struct NoexceptExplicitClass
+  {
+    NoexceptExplicitClass(double&) noexcept(true);
+    explicit NoexceptExplicitClass(int&) noexcept(true);
+    NoexceptExplicitClass(double&, int&, double&) noexcept(true);
+  };
+
+  struct ExceptExplicitClass
+  {
+    ExceptExplicitClass(double&) noexcept(false);
+    explicit ExceptExplicitClass(int&) noexcept(false);
+    ExceptExplicitClass(double&, int&, double&) noexcept(false);
+  };
+#endif
+
   struct NType   // neither trivial nor standard-layout
   {
     int i;
Index: testsuite/20_util/is_nothrow_constructible/value.cc
===================================================================
--- testsuite/20_util/is_nothrow_constructible/value.cc	(revision 0)
+++ testsuite/20_util/is_nothrow_constructible/value.cc	(revision 0)
@@ -0,0 +1,78 @@ 
+// { dg-options "-std=gnu++0x" }
+
+// 2010-06-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+// 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 <type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::is_nothrow_constructible;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass,
+	   double&>(true)) );
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass,
+	   int&>(true)) );
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass,
+	   double&, int&, double&>(true)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass,
+	   double&>(true)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass,
+	   int&>(true)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass,
+	   double&, int&, double&>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass,
+	   void*>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass>
+	   (false)) );
+  VERIFY( (test_property<is_nothrow_constructible, NoexceptExplicitClass,
+	   int, double>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass,
+	   void*>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass>
+	   (false)) );
+  VERIFY( (test_property<is_nothrow_constructible, NothrowExplicitClass,
+	   int, double>(false)) );
+
+  VERIFY( (test_property<is_nothrow_constructible, ExceptExplicitClass,
+	   double&>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, ExceptExplicitClass,
+	   int&>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, ExceptExplicitClass,
+	   double&, int&, double&>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, ThrowExplicitClass,
+	   double&>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, ThrowExplicitClass,
+	   int&>(false)) );
+  VERIFY( (test_property<is_nothrow_constructible, ThrowExplicitClass,
+	   double&, int&, double&>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/20_util/is_nothrow_constructible/requirements/typedefs.cc
===================================================================
--- testsuite/20_util/is_nothrow_constructible/requirements/typedefs.cc	(revision 0)
+++ testsuite/20_util/is_nothrow_constructible/requirements/typedefs.cc	(revision 0)
@@ -0,0 +1,36 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// 2010-06-09  Paolo Carlini  <paolo.carlini@oracle.com>
+//
+// 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/>.
+
+// 
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::is_nothrow_constructible<int, int>   test_type;
+  typedef test_type::value_type                     value_type;
+  typedef test_type::type                           type;
+  typedef test_type::type::value_type               type_value_type;
+  typedef test_type::type::type                     type_type;
+}
Index: testsuite/20_util/is_nothrow_constructible/requirements/explicit_instantiation.cc
===================================================================
--- testsuite/20_util/is_nothrow_constructible/requirements/explicit_instantiation.cc	(revision 0)
+++ testsuite/20_util/is_nothrow_constructible/requirements/explicit_instantiation.cc	(revision 0)
@@ -0,0 +1,31 @@ 
+// { dg-options "-std=gnu++0x" }
+// { dg-do compile }
+
+// 2010-06-09  Paolo Carlini  <paolo.carlini@oracle.com>
+
+// 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/>.
+
+// NB: This file is for testing type_traits with NO OTHER INCLUDES.
+
+#include <type_traits>
+
+namespace std
+{
+  typedef short test_type;
+  template struct is_nothrow_constructible<test_type, test_type>;
+}
Index: testsuite/20_util/make_signed/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_signed/requirements/typedefs_neg.cc	(revision 160490)
+++ testsuite/20_util/make_signed/requirements/typedefs_neg.cc	(working copy)
@@ -48,8 +48,8 @@ 
 // { dg-error "instantiated from here" "" { target *-*-* } 40 }
 // { dg-error "instantiated from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 617 }
-// { dg-error "declaration of" "" { target *-*-* } 581 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 639 }
+// { dg-error "declaration of" "" { target *-*-* } 603 }
 
 // { dg-excess-errors "At global scope" }
 // { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
===================================================================
--- testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc	(revision 160490)
+++ testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc	(working copy)
@@ -48,8 +48,8 @@ 
 // { dg-error "instantiated from here" "" { target *-*-* } 40 }
 // { dg-error "instantiated from here" "" { target *-*-* } 42 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 540 }
-// { dg-error "declaration of" "" { target *-*-* } 504 }
+// { dg-error "invalid use of incomplete type" "" { target *-*-* } 562 }
+// { dg-error "declaration of" "" { target *-*-* } 526 }
 
 // { dg-excess-errors "At global scope" }
 // { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/declval/requirements/1_neg.cc
===================================================================
--- testsuite/20_util/declval/requirements/1_neg.cc	(revision 160490)
+++ testsuite/20_util/declval/requirements/1_neg.cc	(working copy)
@@ -19,7 +19,7 @@ 
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 654 }
+// { dg-error "static assertion failed" "" { target *-*-* } 676 }
 // { dg-error "instantiated from here" "" { target *-*-* } 30 }
 // { dg-excess-errors "In function" }